Gunnar Lium
Gunnar Lium

Reputation: 6073

How can I include javascript in content loaded with jquery using $.ajax

I want to load some HTML which include a bit of javascript, using jQuery. I've tried using both load() and ajax(). The HTML is inserted nicely into the DOM, but any script-tags seems to be filtered out. If I alert() the returned HTML, the scripts are included, but when i use html() or append(), the scripts are missing.

Any ideas?

Upvotes: 2

Views: 777

Answers (2)

karim79
karim79

Reputation: 342695

You should use $.getScript to load and execute remote Javascript:

Loads, and executes, a local JavaScript file using an HTTP GET request.

Example:

$.getScript("test.js", function(){
  alert("Script loaded and executed.");
  $('#myDiv').load('some.html');
});

Upvotes: 3

jerjer
jerjer

Reputation: 8766

try using:

$.get("url.php",{param1:'1',param2:'2'},function(result)){
    $('#somediv').html(result);
}

p.s. please use the latest jquery version

Upvotes: 0

Related Questions