0

Possible Duplicate:
Jquery load with script tags

In a PHP page I'm trying to insert some content using jquery with an onclick call into a DIV called 'content'.

<div id="content" class="content"></div>

The version of jquery I'm calling is:

//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

When the link is clicked it calls the following javascript:

function loadA(a_variable) {
    $("#content").load("build.php?variable=" + a_variable); 
}

The build.php page does a few things and tries to include some text insert a javascript:

echo "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";

echo "<script type='text/javascript' src='script_name.js'></script>";

echo "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";

Everything works except the script is removed and doesn't appear in my client. Any idea why this is happening and how it can be fixed?

3
  • are you certain that the script actually doesn't execute? The markup may be removed, but the js should still execute api.jquery.com/load Use firebug etc to see if the browser is requesting /script_name.js Commented Jan 8, 2013 at 11:12
  • add alert("From Added Script"); in your script_name.js and check if that gets executed on load Commented Jan 8, 2013 at 11:18
  • why don't you try to use getScript() function. Commented Jan 8, 2013 at 11:20

2 Answers 2

1

jQuery doesn't actually append <script> elements to the DOM. Instead, it just evals the contents of the script. Since it isn't in the DOM, so you cant find it, though you could check if your php echo'ed content does consist of script tag or not by checking the content obtained from .load like:

$("#content").load("build.php?variable=" + a_variable, function(data) {
  console.log(data); // check if you see script in the content or not
}); 
Sign up to request clarification or add additional context in comments.

Comments

0

Try to add HTML tags to your PHP for example:

echo "<html><body>data.<script></script>...</body></html>"

Use any network sniffer (like FireBug, or if you using chrome the Network tab in the developer console), to see the what you get from the php, maybe there is an 404 error in your page)

1 Comment

This will create nested <html> and <body> elements in the DOM, is that valid? Unless jQuery is smart enough to filter them out, in which case they're not needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.