1

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 ?

Frits
  • 7,341
  • 10
  • 42
  • 60
Ricky
  • 19
  • 2
  • 10

1 Answers1

5

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.

Cashif Ilyas
  • 1,619
  • 2
  • 14
  • 22
  • so, i just can't run it using chrome, but when i deploy this website online, it will work ? – Ricky Jun 22 '16 at 06:30
  • @RickyGauni You can't run it as local files. Issue is not specific to chrome. And you don't have to put it online, you can also use a local server like XAMP to test it. – Cashif Ilyas Jun 22 '16 at 06:34
  • actually as the previous answer suggest, i had no problem opening it using mozilla .-. but i will try to learn all about XAMP later today, thanks – Ricky Jun 22 '16 at 06:36
  • @RickyGani That's right. Mozilla's policy on the issue is much relaxed as compared to Chrome: https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs – Cashif Ilyas Jun 22 '16 at 06:39
  • Well, anyway, thanks for clearing up this problem to me, looking forward to learn about XAMP :) – Ricky Jun 22 '16 at 07:17