1

i am stuck here with a little jQuery Problem! I am using this code to load the content of a html file (bio.html) into a div containter (content). This works in IE8 and Firefox 6, but not in Google Chrome!

Any ideas what i might be doing wrong? Heres the Code:

<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="./js/jquery.min.js"></script>
</head>

<div id="content">
    hi
</div>

<script type="text/javascript">

    $(document).ready(function(){   

        $('#content').load('./html/bio.html');

    });

</script>

jkid
  • 11
  • 1
  • 2
  • Any JavaScript console errors? Have you tried it with adding a 'complete' callback (the 3rd parameter in load())? Does the developer tool Network tab show an AJAX request? If so, check if it is returning an HTTP error code. (There is a same-domain security policy for most browsers--but it looks like that is not your problem here since you're using relative URIs.) – Jon Adams Sep 02 '11 at 18:11
  • This question has been asked a few times. I posted this answer on a different question. http://stackoverflow.com/a/24376097/756329 – Joseph Hansen Jun 23 '14 at 23:22

4 Answers4

2

The issue is just that .load() for local files is blocked by Chrome for security reasons. If you are using it on a server it works, given that all of the files originate from the same place.



To enable a working version locally, start Chrome with a command line flag enabled by trying:

In Mac OS X, quit Chrome, enter in Terminal:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files

In Windows, quit Chrome, enter in Command Prompt:

chrome.exe --allow-file-access-from-files

(Maybe you actually have to have the path... I don't think so. If so, you'll have to find it yourself.)

In Linux, quit Chrome, enter something like this into the terminal:

/usr/bin/google-chrome --allow-file-access-from-files
Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68
1

It works fine for me in chrome

http://sandbox.phpcode.eu/g/512ef/2

try to look at developer's inspect tool if you see any unexpected errors. Be sure you're in http:// or https:// protocol, file:// will not work

genesis
  • 50,477
  • 20
  • 96
  • 125
1

.load() will not work in Chrome if you are loading files from your local system.

Putting it on a server will resolve the issue.

Jack
  • 8,851
  • 3
  • 21
  • 26
1

Sounds like you are running this locally, is that correct? Chrome has some security features that prevent JQuery's load method to fail when run from a local harddrive. Try putting it on a server somewhere.

damon
  • 1,087
  • 9
  • 12