javascriptjquerygetcufon

Calling Javascript in a page after it's been loaded by jQuery GET


Imagine a normal page calling javscript in head. The trouble is some of the content isnt loaded untill i click on a link. Subsequently when this link loads the content it wont work. This is because i guess the javascript has already been run and therefor doesnt attach itself to those elements called later on. There is only standard html being called.

So for example this is the code which calls my external html.

$.get('content.inc.php', {id:id}, function(data){
        $('#feature').children().fadeTo('fast', 0).parent().slideUp('slow', function(){
           $(this).html(data).slideDown('slow');
        });
    });

If the html i was calling for example and H1 tag was already in the page the cufon would work. However because i am loading the content via the above method H1 tags will not be changed with my chosen font.This is only an example. The same will apply for any javascript.

I was wonering whether there is a way around this without calling the the javascript as well the html when its received from the above function


Solution

  • Does Cufon.refresh() do what you want?

    As you said Cufon was just an example, I'd also suggest a more general:

    $.get(url, options, function(html, status) {
        var dom = $(html);
        // call your function to manipulate the new elements and attach
        // event handlers etc:
        enhance(dom);
    
        // insert DOM into page and animate:
        dom.hide();
        $target_element.append(dom); // <-- append/prepend/replace whatever.
        dom.show(); // <-- replace with custom animation
    });