简体   繁体   中英

I am using javascript to load some html page in another html page. Its working fine in Mozilla Firefox but not working in Google Chrome and IE10

I wrote code for dropdown Menu and want to insert that code in other html files. DropDown Menu code.

http://jsfiddle.net/techspartan/49Bpb/

For inserting the above HTML code into other HTML files I am using this code:

  <html>
     <head>
       <script src="jquery-2.0.3.js"></script>
       <script type="text/javascript">
         $(document).ready(function(){
           $('#topdiv').load('index.html');
         });
       </script>
     </head>

    <body>
     <div id="topdiv">
     </div>
    </body>
 </html>

Basically I want to declare my DropDownMenu code at one location so that if I make changes in menu code than I don't have to edit every HTML file that has the DropDown.

The above code is working in Firefox but nothing is shown in Chrome and IE-10.

Are you working locally on your machine, without any webserver? Chrome does not allow loading files via AJAX from your file system (see bugreport ).

You may use XAMPP or something similar to serve your files via a local webserver.

If you are on Windows, XAMPP is probably the easiest way to get your local webserver up and running: http://www.apachefriends.org/en/xampp.html

On Mac, you may also use MAMP http://www.mamp.info/en/index.html

You may also force Chrome to allow local file access on Windows if you are starting it with --allow-file-access-from-files , more information in this stackoverflow question

For what it's worth, I have code that uses jQuery().load() to inject content into a page, and it work just fine.

If this is static content that is meant to be a standard part of your page, then the other answers/comments saying to do it on the server are probably right; stuff like that is generally better to be included on the server, because it will make your site perform a lot better than doing it on page load via Javascript. (in fact, loading a static menu this way is likely to give your site a noticable performance problem when users load the page; be warned!).

However in general the technique of dynamically adding content to a page using Javascript is perfectly valid, and commonly used, so I'll answer the question based on that.

There's nothing that I can see that's specifically wrong with the minimal example you provided, except for a missing Doctype, so I'm going to guess that's probably your issue:

If you don't have a doctype, the browser will render the page in Quirks mode. And jQuery is not designed to work in quirks mode.

Solution: Add the following line to the top of your code and try it again:

<!DOCTYPE html>

You may also want to check that IE isn't showing your page in compatibility mode as well, because that might also cause problems. If it is, you could also add an X-UA-Compatible meta tag to your page's <head> section to force IE into standards mode:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

Finally, if you need to support IE8 or earlier, you should switch from jQuery v2 back to the latest release of jQuery v1 (currently 1.10.2), as jQuery v2 does not work with IE8 and earlier.

Hope that helps.

The issue you are having is not due to anything wrong with your code, but with security policies of modern browsers. Here's what happens on your development machine:

Your browser loads your local HTML file.

Your browser executes the javascript, which tries to access a file on your machine.

Your browser says, "NO!" Because this is a huge security error - without this policy websites could read through every file on your hard drive or silently send copies of any of your private information to their servers, just because you visited a site with javascript enabled. BAD!

There are some ways to try to tell your browser "No, it's ok, I want to allow this..."...but you know, this has become exceedingly difficult as it often silently breaks with new browser versions. I've slammed my head against the wall way too often, so I might suggest you skip trying to make your browser OK with what you are trying to do.

Now, why does this work on a live site? Here's what happens.

Your browser loads a website.

Your browser executes the javascript.

The script asks for a file to be loaded/accessed from a website.

Your browser says..."well, we're already on this website, so sure! Load all the files you want from that web server!" And your browser kindly gets the file, and returns it to your script, where you can painlessly include the HTML to your hearts content.

To make this work on your development machine, you have ultimately 3 choices:

1) Upload the files to a web server, then do your testing there.

2) Make your own "localhost" web server. You then access your site with something like localhost/index.html - etc. This is just enough to prevent the browser from shutting down your file load requests, because you are requesting an HTTP operation, note a FILE operation.

3) Try to force your browser to allow these requests. Details vary by browser, some browsers won't let you do this at all, and I've given up on doing this myself.

The hidden 4th choice is using HTML5 File System features, but with such poor support for technology I suggest you not even try it - the bug you are facing is purely with your development machine, so changing the technology you are using purely for a minor development convenience seems silly.

Severin provides links to the excellent XAMPP and MAMP software packages, which are the easiest way of getting yourself a good development localhost server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM