3

Before someone mark this question as duplicate, I did a lot of investigation on this matter and I couldn't found the solution to my problem yet. So, I have these header.html and footer.html files, and I want to load their contents into a new html page (trying to not repeat code). I Know that I can do these in php in a very easy way, but I'm trying to do it with JQuery (just started to dig into it). This is my code so far:

<!DOCTYPE html>
<html>

    <head>
            <link rel="stylesheet" type="text/css" href="./style.css">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

             <script> 
                $(function(){
                    $('#filename').load("footer.html");
                });
            </script>       
    </head>

    <body>

        <div id="filename"></div>

    </body>
</html>

I am getting the following error:

jquery.min.js:5 XMLHttpRequest cannot load file:///C:/Users/...../footer.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.send @ jquery.min.js:5ajax @ jquery.min.js:5b.fn.load @ jquery.min.js:5(anonymous function) @ index.html:10c @ jquery.min.js:3fireWith @ jquery.min.js:3ready @ jquery.min.js:3H @ jquery.min.js:3

Brad
  • 159,648
  • 54
  • 349
  • 530
  • You really shouldn't be loading headers and footers this way; It's only increasing the number of requests your page has to make. What you should have is the header and footer in a "base" page which imports the content pages. – Soviut Sep 11 '16 at 02:04
  • What's the exact cross-origin error you're getting? Can you show us the response headers for the `footer.html` request? – Brad Sep 11 '16 at 02:07
  • Depending on the structure of your site, if you want static pages but built in a way where you use a common template, you might consider a bundler like Webpack. It's a way to crank out static HTML with the full page (so you can easily host on any CDN) but by only having to maintain a common template. – Brad Sep 11 '16 at 02:09
  • @Brad this is the error mesage http://pastebin.com/fTD5L8p1 –  Sep 11 '16 at 02:13
  • @Brad I'm getting used to this, thank you for the heads up –  Sep 11 '16 at 02:25

2 Answers2

1

You can't make AJAX requests to your local drive... there's no HTTP server, therefore no HTTP request can be made. The cross-origin error you're getting is slightly misleading.

Put your page on a server.

See also: https://stackoverflow.com/a/20578692/362536

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
-1

The reason why your code was not working is because you've placed the Jquery before the element has been loaded into the site causing it to not work.

You could use something like this to fix it:

$( document ).ready(function() {
    $('#filename').load("footer.html");
});

This will wait until the page's elements have already been loaded into the page and then it will load the footer.html content into the element with the ID of filename.

FluxCoder
  • 1,266
  • 11
  • 22