4

If I click on button javascript will be append to iframe.

example:

<button id="run">Click me!</button>
<textarea></textarea>

var iframe = here_is_iframe_object;

$('#run").click(function(e) {
    $(iframe).find("script[id=myScript]").remove();
    $(iframe).append("<script id='myScript'>"+ $("textarea").val() +"</script>");
});

If I put into the textarea setInterval with alert for example and after that put simple alert, it will be execute setInterval with alert and new alert at the same time.

setInterval(function() {
    alert(true);
}, 5000);

How to stop previous javascript and execute only new code?

Thanks in advance.

1
  • Somehow this problem seems a bit backwards to me. Is the content of the iframe on the same domain as the rest and under your control? Commented Jul 19, 2014 at 10:17

1 Answer 1

0

I think we do need a little more info about your code (ie context). For what I understood, this snippet works as expected (when typos are corrected) But be carefull, I'm no specialist but I think that you're in the case eval() can be dangerous : Look at this answer about using eval().

To stop a setInterval() timer, use clearInterval() (https://developer.mozilla.org/en-US/docs/Web/API/Window.clearInterval)

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.