Your question is one that crops up frequently on this list.

1) The $(document).ready() only fires *once* per page load--and that's when
the root document's DOM is ready.

2) The $.load() method loads a DOM fragment--not a complete document object.
In a nutshell, this means your just added some additional HTML to an
existing page.

So, putting a $(document).ready() statement inside a page inserted into the
DOM via the $.load() method is not expected to work.

The correct way to trigger code off after the DOM fragment has been inserted
into the document is to use the callback in the $.load() method.

$("#feeds").load("feeds.html", null, function (){
        alert("Hey, I loaded!");
});

One of the nice benefits to the callback method as well is all your JS is on
one spot--not divided amongst templates.

-Dan


>-----Original Message-----
>From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
>Behalf Of [EMAIL PROTECTED]
>Sent: Wednesday, July 25, 2007 11:25 AM
>To: jQuery (English)
>Subject: [jQuery] $(document).ready behavior when using $("#div").load
>
>
>I am using some javascript in the $(document).ready function to decide
>which content gets shown.  My problem is that this never gets executed
>when i load this page into a div on another page.  The below has been
>stripped of some other things i am doing, FYI.
>This is in the page with the content:
>
>$(document).ready(function(){
>
>                       if(notvalidated){
>                               var thisContent = 'You are not authorized';
>                               $("body").empty();
>                               $("body").append(thisContent);
>                       }
>});
>
>And this is in the page where i would load the content:
>
>$(document).ready(function(){
>                       $("#clickMe").click(
>                               function(){
>
$('#contentDiv').fadeOut(1200,function() {
>
>       $('#contentDiv').load('content.html').fadeIn(1200);
>                                       });
>                                       return false;
>                               });
>});
>
>When i just visit the content page, i get "You are not authorized".
>But when i load the content into the div, i get the full content,
>which leads me to believe that the document.ready function is not
>running at the time the content is pulled.  Does anyone have any
>pointers?  Am I missing something?
>
>Regards,
>-jesse-
>


Reply via email to