One weird problem about $ .load ()
This is the file that I use $ .load () to load into the DOM:
<script type="text/javascript"> $('.close').click(function() { alert(1) }); </script> <div class="close"> click me </div> Say, it seems to me that the <script> will automatically linger when it is loaded , is the $ .load () function?
If so, how is this implemented?
Wonderful!
I read the jQuery source and here is what I found:
(line numbers refer to uncompressed jQuery 1.3.2)
jQuery.loadeventually gets the response and calls the jQueryhtmlmethod with the result to insert it. (around line 3267).jQuery.htmlthen calls the jQueryappendmethod. (line 488)jQuery.appendthen calls thedomManipmethod with a callback function that inserts DOM nodes. (line 253)domManip(on line 514) is a bit more complicated, but ultimately it actually passes the DOM nodes for the callback to be inserted, then it callsevalScriptfor each script after the DOM nodes are inserted, regardless of their order in the loaded html. (line 526).
Consequently, jQuery really does run delayed scripts!
use the source, Luke.
"the script will not work as expected if you access the file directly using a browser (there is no warning when you click on it). But if you upload it to another page using $ .load (), it will work (warning when pressed). - Shore "
When you download a file in the browser as it is, the reason that there is no warning when you click on it is because when the browser reads the line " $('.close')... ", the DOM object of the class " close " doesn't exist yet because it comes after the script tag.
You can solve this:
$(document).ready(function(){$('.close').click(function() { alert(1) });}); As for the delay part, this is because when using $ .load (), the function first downloads the file and then puts the source in the DOM object. After placing the source in the DOM object, it will parse the <script> tags in your file. That is why your click event is successfully bound to your DOM objects with class close