I am trying to load one page into another using the .load () method. This loaded page contains a script that I want to execute when it finishes loading. I put together an example with barebones to demonstrate:
Index.html:
<html> <head> <title>Jquery Test</title> <script type="text/javascript" src="script/jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#nav a').click(function() { $('#contentHolder').load('content.html #toLoad', '', function() {}); return false; }); }); </script> </head> <body> <div id="nav"> <a href="content.html">Click me!</a> </div> <hr /> <div id="contentHolder"> Content loaded will go here </div> </body> </html>
content.html:
<div id="toLoad"> This content is from content.html <div id="contentDiv"> This should fade away. </div> <script type="text/javascript"> $('#contentDiv').fadeOut('slow', function() {} ); </script> </div>
When you click on the link, the content should load, and the second paragraph should disappear. However, this is not performed. If I adhere to a simple warning ("") in the script content.html, it also fails.
However, if I do away with the #toLoad selector in the .load () call, it works fine. I am not sure why this is so, since the block is explicitly in the scope of div #toLoad. I do not want to avoid using a selector, since in fact content.html will be a full HTML page, and I only want to select a part from it.
Any ideas? If the script from content.html was in the .load () callback, it works fine, but I obviously don't want this logic to be contained in index.html.
Could I use the .getScript () callback to load "content.html.js" after that and have logic there that seems to work? I would prefer to save the script in content.html, if possible, so that it works fine when loading too. In fact, I could do it anyway, but I would like to know why this is not working.
javascript jquery
Kasaku
source share