11

Ok the following HTML code is parsed when I load a page:

<div id="clip-wrapper"> 
    <script type="text/javascript"> 
        alert("bla");
    </script> 
</div>

This obviously leads to an alert saying "bla", after my AJAX call, the parsed HTML code looks like this:

<div id="clip-wrapper">
<script type="text/javascript">
    alert("ajaxbla");
</script>
</div>

This on the other hand, doesn't result in an alert. Why? And how do I fix this?

0

4 Answers 4

10

If there are any scripts loaded into your dom from an Ajax call, they will not be executed for security reasons.

To get the script to execute, you'll need to strip it out of the response, and run it through eval

Ideally though, you'll want to run your script as a callback for when your ajax request completes. If you're using jQuery, this would be the success event of your ajax call, and if you're doing it natively, it would be the readyStateChange event of your xhr object.

EDIT

If you really want to opt for the eval option, you could try something like this:

document.getElementById(contentDiv).innerHTML = xmlHttp.responseText;
eval(document.getElementById("clip-wrapper").innerHTML);
Sign up to request clarification or add additional context in comments.

7 Comments

Can you eleborate how I can get it out of the response? And run it through eval afterwards? I'm using the following code: function stateChanged() { if (xmlHttp.readyState == 4) document.getElementById(contentDiv).innerHTML = xmlHttp.responseText; } where contentDiv is clip-wrapper in this case. (is it as simple as eval(xmlHttp.responseText); ?)
Ok I think I have it right, but it still doesn't work: if (xmlHttp.readyState == 4) { document.getElementById(contentDiv).innerHTML = xmlHttp.responseText; eval(document.getElementById(contentDiv).innerHTML); } am I missing something? Thanks for the help, really appreciate it!
I don't think eval likes the script tags in there - can you send back the response without them?
Doesn't that imply that the div will only be filled with alert("msg"); so it won't run as javascript code? Or am I understanding you wrongly?
if it's just filled with alert("msg") you can pass that to eval, and it will definitely work -- eval("alert('msg');"); will work
|
1

put you code in

$(document).ready(function()
{
  alert(msg);
});

or make use of readysatchange event of XMLHttp object.

XMLHttpobject.readyStateChange( furnction() { alert('msg'); } );

Edit

if you are using jquery than go for

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  }
});

here in sucess event you can write your code which get executed once you are done with the ajax.

5 Comments

I've tried your suggestion, it parses the following HTML code: <div id="clip-wrapper"><script type="text/javascript"> $(document).ready(function() { alert("trolol"); }); </script></div> but it doesn't give an alert. PS. how to get code in comments?
It's included, as I'm using Wordpress.
I haven't -1'ed you, someone else did. I didn't +1 you either, sorry.
@peterbond - have you tried ajax call function of edited answer
No, I haven't, I'm not using jQuery sorry. I +1'ed you (it atleast undid the -1 of someone else) for your effort, as it seems the edited code will work for jQuery, using the success. I've used the solution provided by @Adam Rackis and it works like a charm, also with my real script (loading a movie in jwplayer, depending on what the user clicked on).
1

any code inside body in between script tag will executed only while loading the document.

in ajax call,make use of callback or success: or oncomplete: to handle the ajax response if you are using jQuery.

Comments

0

You mean the second html code is coming as an ajax response?? Did you even inserted the html to the current page?

Something like,

document.body.innerHTML = ajax_response;

But even with innerHTML replacement I don't thing the script inside it will be auto executed. The browser will parse the script but won't auto execute it. (For eg, if you have a function definition in the html, after the ajax call you will be able to call those functions)

Another option is to use document.write, it will replace the whole page content with the new html and the script tags in it will be auto executed.

document.write(ajax_response);

Hope this helps,

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.