0

I use jQuery to make an ajax request and add the code to my page. The code that I add to the page not only contains HTML, but also javascript.

Apparently the javascript-code is not being called! How can I force to my newly added javascript sourcecode being executed?

2
  • 4
    We can't know what you did wrong if you don't show your code. Commented Aug 8, 2011 at 17:00
  • Wrap you script in a eval() command, be careful with script injection! Commented Aug 8, 2011 at 17:02

1 Answer 1

1

You probably are using the jQuery(element).load(url) method.

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as , , or elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

Try to scope the content that you are receiving from the server, for example:

// will load only the content inside the element with id="container"
jQuery(element).load("yoururl #container"); 

You probably should try to execute the JS code from the script, using the complete callback, for example:

jQuery(element).load("yoururl #container", function(response, status, xhr) {
    do_stuff_after_load();
}
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.