Ricky Gani Ricky Gani - 3 months ago 32
HTML Question

Can't use w3school HTML include

So here's the link to the original w3school article about HTML include using their .js file link.


I literally copied everything from the w3school example, but got this error :


w3school.js:131 XMLHttpRequest cannot load file:///C:/Users/KBS-3/Desktop/project_1/html/header.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.


Here's my index.html



<!DOCTYPE html>
<html>
<script src="http://www.w3schools.com/lib/w3data.js"></script>

<body>

<div w3-include-html="content.html"></div>

<script>
w3IncludeHTML();
</script>

</body>
</html>





and content.html



<h1>HELLO WORLD</h1>





Any ideas to resolve this ?

Answer

As the error suggests, you are trying to include a local html file file:///C:/Users/KBS-3/Desktop/project_1/html/header.html. using javascript which is not permitted due to security reasons. (Imagine, if it were allowed, websites could read any of your local hard drive data).

Solution: Move your website to localhost i.e. use XAMP or a real server and you'll be fine.