Dorjan
Dorjan

Reputation: 2037

Question about Javascript (Jquery) and GET

After getting a new page with $.get none of the javascript will run on the new page.

Is there a way to make javascript use the new page too?

Many thanks

Edit: Example:

$(function() {
    $('.viewPage').click(function() {
        $('#mainarticle').fadeOut('slow')
        $.get($(this).attr('href'), { js: "1" }, function(data) {
            $('#mainarticle').html(data).fadeIn('slow');
        });
        return false;
    });
});

Now this works fine however, the new page's anchor tags won't trigger (lets say it has a .viewPage).

I hope that clarify's the issue.

Upvotes: 1

Views: 132

Answers (2)

karim79
karim79

Reputation: 342635

You need to bind events to your anchors using live:

$('a.something').live("click",function() {
    alert('this will still work after a.something has been replaced via ajax');
});

Another way using $.get's callback:

$.get( "page.html", function(data) {
    $('#someDiv').html(data);
    $('a.something').click(function() {
        alert('this will still work after a.something has been replaced via ajax');
    });
});

Now that I've seen your code:

$(function() { 
    $('.viewPage').live("click",(function() { 
        $('#mainarticle').fadeOut('slow') 
        $.get($(this).attr('href'), { js: "1" }, function(data) { 
            $('#mainarticle').html(data).fadeIn('slow');
        }); 
        return false; 
    }); 
});

Upvotes: 2

Noon Silk
Noon Silk

Reputation: 55072

Yep; there is another jquery ajax method that will take the returned script from your page and execute it. Check the jquery docs.

Upvotes: 0

Related Questions