views:

277

answers:

4

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 funtion

+2  A: 

If you want to attach events to elements on the page that are dynamically created take a look at the "live" keyword.

$('H1').live("click", function() { alert('it works!'); });

Hope this is what you were looking for.

griegs
A: 

You can attach event handlers to the data that you get via the get() inside of the callback function. For example

$.get('content.inc.php', {id:id}, function(data){
    $('#feature').children().fadeTo('fast', 0).parent().slideUp('slow', function(){        
        $(this).html(data).find('a').click(function(e) {
            // specify an event handler for <a> elements in returned data
        }).end().slideDown('slow');
    });
});

live() may also be an option for you, depending on what events you want to bind to (since live() uses event delegation, not all events are supported).

Russ Cam
I am trying to run this code on any html which is pulled inShadowbox.init();
Andy
@Andy - re-reading your question, I'm not sure now that I understand the problem. Are you saying that you have a CSS stylesheet with a defined style for `<h1>` elements and that this style is not being applied to `<h1>` elements that are loaded into the DOM via `get()`?
Russ Cam
Sorry its hard to explain. Take cufon for example. It runs a javscript line like: Cufon.replace('h1');. The trouble is this is ignored if i pull in the html from the GET. If i was to simply put an H1 tag directly in the page it would work perfectly.
Andy
I'm not familiar with cufon, but it sounds like you may need to call cufon on the newly inserted DOM elements, based on the limited reading I just did on the project page. Can you show the code that you have for cufon as this may show up some further clues.
Russ Cam
A: 

Andy try this. It will call the Cufon code after each AJAX request is complete and before the html is actually added to the page.

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

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
});
searlea