0

im working with jquery load function which require to fill content of a div from page to another page.

first.html

  <!DOCTYPE html>
 <html>
 <head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <script>
 $('#getselect').find('option:selected').text();
 </script>
 </head>
<body>
<div id="getdiv">Not loaded yet.</div>
</body>
 </html>

second.html

 <!DOCTYPE html>
  <html>
  <head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  </head>
  <body>
   <b>Footer navigation:</b>
  <div id="getit"></div>
   <div id="getitselected"></div>
   <script>
  $("#getit").load("first.html #getdiv");
  </script>
  </body>
 </html>

The script works fine in firefox but not working in chrom. please help me.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Siri Sireesha
  • 21
  • 1
  • 3
  • 9

6 Answers6

1

Try wrapping your code in $(document).ready()

$(document).ready(function(){
   $("#getit").load("first.html #getdiv");
});
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

If you do not host your site on a server and you just try to open an .html file from a browser, in that case chrome cannot access another file in your filesystem. You have to deploy your project to a server e.g wamp and test it. You' ll see chrome works fine.

Lem
  • 419
  • 5
  • 4
0

This is an AJAX request and chrome won't run it if you try to directly access first.html from second.html because of same-origin-policy

Put it on a server (local/non-local) and try to run it like localhost:3000/testProject/second.html , it'll work fine.

Harsha Venkataramu
  • 2,887
  • 1
  • 34
  • 58
0

Or you can start Chrome like this

cd C:\Program Files\Google\Chrome\Application
start chrome.exe C:\second.html --disable-web-security
gaepi
  • 404
  • 3
  • 14
0

Use preventdefault():

$(document).ready(function(){
   event.preventDefault();
   $("#getit").load("first.html #getdiv");
});
TLama
  • 75,147
  • 17
  • 214
  • 392
Ali.azimi
  • 93
  • 2
  • 9
0

See https://stackoverflow.com/a/2990622/1123295 and http://www.chrome-allow-file-access-from-file.com/

You will need to run chrome with --allow-file-access-from-files to get jquery's load function to work locally.

If in Windows 7+, pin Chrome to your taskbar and right click on the icon, then right click on "Google Chrome" and select Properties. Then change the target to have the new flag, something like this:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files

Now every time chrome launches, it will use the new policy.

Community
  • 1
  • 1
Allen Pestaluky
  • 3,126
  • 2
  • 20
  • 22