1

Firstly, forgive me if the title is vague, can't seem to think of the right question to ask.

Anyway, I'm trying to load an external html with JQuery using ajax load function like so:

$("a").click(function(){
    $(".contentWrapper").load(pageURL+" .externalContent","ajax=true",function(){
    //some more code here
    });
});

What this does (according to how I understand it) is load the contents of an element with a class .externalContent from the page pointed to by pageURL after parsing it from the returned html data.

My problem is .externalContent has some scripts associated with it in its original page. What should I do to have those scripts executed once the .externalContent has been loaded.

Simply putting the script in the calling page doesn't work as the div does not exist when the script gets called on document.ready, at least from how I understand it.

I've also tried putting the scripts inside the ajax success option but I cant seem to get it working. Sorry if I'm not making much sense I've been at this all night. I'm new to JQuery and would appreciate any help, including better ways to code things like this. Thanks!

2
  • 2
    api.jquery.com/jQuery.getScript is for loading and executing JavaScript via AJAX. Commented Jan 18, 2013 at 19:45
  • Thanks Blazemonger! While getScript didn't work for my problem it prove to be something I need to know as well. Commented Jan 18, 2013 at 20:05

1 Answer 1

1

The ajaxComplete function could be used here or the delegate function

http://api.jquery.com/ajaxComplete/

$(".contentWrapper").ajaxComplete(function(){
 //execute even handlers here
});

or

jquery delegate This will be universal to the selector, even after its loaded ajax http://api.jquery.com/delegate/

$("table").delegate("td", "click", function() {
  $(this).toggleClass("chosen");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.