views:

47

answers:

3

So I have a script like this

    <script type="text/javascript"
            src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"&gt;
    </script>
    <script type="text/javascript">
       $(document).ready(function(){
       $("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");

       $("a[target!='_blank'][target!='_top']").click(function(){

       var url=$(this).attr("href")+'?jquery=1';
       ajaxpage(url,'actualcontent');

       window.location.hash=$(this).attr("href");
        return false;
    });


});
    </script>

and it works great. It means all my links load dynamically within the DIV - awesome. But, the links loaded in those div, don't load dynamically when clicked. and if I include this script in it, it still doesn't work. And on a similar note, is there a way of making javascript in pages, which have been loaded dynamically, work? Without including it in the original header?

Thanks.

+2  A: 

Disclaimer: To use this solution, you'll need to upgrade to jQuery 1.3 or jQuery 1.4+
(But you should, there are many performance improvements and bug fixes since 1.2.6)


Instead of .click() you can use .live() here, like this:

$("a[target!='_blank'][target!='_top']").live('click', function(){

.live() works on dynamically added elements as well, since it's listening for the bubbling click event at document, rather than being a handler on the element itself.

Nick Craver
I still seem to have the same problem? Would this script have to be included on the newly loaded pages?
Tom
@Tom - Nope, just on the main page...but as I said you're using jQuery 1.2.6 that doesn't have `.live()`, you'll have to upgrade to use it.
Nick Craver
It is useful to explain what the live() function does. From the documentation: "Attach a handler to the event for all elements which match the current selector, now or in the future". This is different from the click() function, because that is only attached to all current hyperlinks.
Marc
Oh, sorry. I didn't notice you said that. That's absolutely awesome - thanks! Really appreciated. So quick as well. :)
Tom
@Marc - I thought the bottom half of the answer did that, with a link as well...but `.live()` is a bit ambiguous from the documentation (IMO), what it *actually* does is put an event handler on `document` and listens for events that bubble, that's what triggers the handler, but the documentation doesn't explicitly state what's going on here. You can also use `$('a', $('#content')[0]).live()` for example to give it a context, it just depends on your usage.
Nick Craver
And cheers Marc for explaining the difference. :) That does help, thanks! :)
Tom
As for my question about loading scripts within the dynamically loaded pages? How would I go about that? Currently scripts within dynamically loaded pages don't work. Thanks again.
Tom
+2  A: 

Not sure what your problem is. You are saying that the links that are added after this function rn do not use this function? hint, you need to rescan the page to update the links in that div OR you can avoid that and use live().

epascarello
A: 

Use .delegate()

// Don't need to put this in a $(document).ready() callback.
$(document).delegate("a[target!='_blank'][target!='_top']", "click", function(e){
    var url=$(this).attr("href")+'?jquery=1';
    ajaxpage(url,'actualcontent');
    window.location.hash=$(this).attr("href");
    e.preventDefault();
    return false;
});

$(document).delegate("a[href*='http://']:not([href*='"+location.hostname+"'])", "click", function(){
    // lazy attr setting, reduces query cost
    $(this).attr("target", "_blank")
});
BGerrissen