0

What I'm trying to do

Load an HTML file into a content block on a page with the .load function in a linked .js file using a local server.

What I'm using

HTML
CSS
Javascript/jQuery
WAMP
Chrome
Windows 10

The problem

I can successfully do this inside the page, but can't get it working when using linked files (as a matter of fact, I can't get any JavaScript to work when I use linked files), and I'd rather be able to maintain a separate .js file.

What I've done

  • checked spelling
  • checked file paths
  • read similar SO questions and comments (didn't help)
  • restarted my computer (why not?)
  • Before using WAMP, tried starting Chrome with local file access allowed. That worked for several minutes... until it didn't anymore.

Notes

  • I'm fairly new to JavaScript and jQuery.
  • The linked .css files have never given me any trouble.
  • Yes, nav.html is in the same directory as index.html.
  • Yes, the js folder is in the same directory as index.html, and design.js is indeed inside that folder.
  • WAMP icon is green and I was able to set up the VirtualHost succesfully.

Code that doesn't work for me

index.html

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
    <script src="js/design.js"></script>
  </head>
  <body>
    <header>
    </header>
    <nav>
    </nav>
    <article>
      <section>
      </section>
    </article>
    <footer>
    </footer>
  </body>
</html>

design.js

$(document).ready(function() {

    loadNav();

});

function loadNav(){

    $('nav').load('nav.html');

}

OR

design.js

$(document).ready(function() {

    $('nav').load('nav.html');

});

Code that works for me

index.html

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
    <script>
      $(document).ready(function(){

        $('nav').load('nav.html');

      });
    </script>
  </head>
  <body>
    <header>
    </header>
    <nav>
    </nav>
    <article>
      <section>
      </section>
    </article>
    <footer>
    </footer>
  </body>
</html>

OR

index.html

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
  </head>
  <body>
    <header>
    </header>
    <nav>
    </nav>
    <article>
      <section>
      </section>
    </article>
    <footer>
    </footer>
    <script>
        $('nav').load('nav.html');
    </script>
  </body>
</html>
heyycap
  • 25
  • 1
  • 8

2 Answers2

1

If you open console you'll see

XMLHttpRequest cannot load file:///.../nav.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

It's about browser politics. It works in Firefox, but not in Chrome. If you want it to work you may run a web server on your local machine to serve the files.

More information:

XMLHttpRequest cannot load file. Cross origin requests are only supported for HTTP

"Cross origin requests are only supported for HTTP." error when loading a local file

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • I am already using WAMP, though (which I mentioned above), with which I have set up a local server. I'm pretty sure I set it up correctly. And it does not work in Firefox either. The next time I am working on this, I will check the console myself, though. – heyycap Aug 22 '17 at 02:35
  • I think it is using port 80, but adding the full path does not do anything for me. – heyycap Aug 27 '17 at 04:55
  • Thanks for troubleshooting with me, @Danil. I found my error was a silly mistake (posted my findings as the answer above). – heyycap Aug 27 '17 at 05:07
0

OP answering own question. I had another function below what I posted above in the JavaScript that was missing an end brace, which caused the entire .js file not to work. Silly mistakes! Everything works, and for the record, it is not necessary for me to include the full path.

heyycap
  • 25
  • 1
  • 8