0

I am preparing an HTML code on memory for an Iframe, when I use append it executes the code.

html = $(parser.parseFromString($("#EHtml").val(), "text/html"));
js = '<script>' + $("#EJs").val() + '</script>';
html.find('body').append(js);

$("#EHtml").val() contains HTML code and the append function does its job but also executes the code. Any thoughts here?

13
  • What's the value of $("#EJs").val() Commented Mar 2, 2014 at 19:53
  • it is js code in a string. it could be: 'alert("a")' Commented Mar 2, 2014 at 19:54
  • once you insert html into the dom with append, any script inside is executed, as if the page was freshly loaded. Commented Mar 2, 2014 at 19:54
  • So, what you want to do ? wait for a time or wait for user click ? Commented Mar 2, 2014 at 19:54
  • 1
    @EnjoysTurtles You don't comment on the question every time you post an answer. The user gets a notification that you posted an answer. Think about it, if there are 100 answers then 100 people will comment My answer accept mine because it's good yes accept it. Commented Mar 2, 2014 at 20:00

5 Answers 5

1

You need to just store a reference to the string of code and do 1 of two things: either do the append interaction only when you want to run the code later, or run eval(jsString) when you want to run it.

Sign up to request clarification or add additional context in comments.

6 Comments

So, I should put the html on the IFrame, and the use append there?
Wherever the code is intended for. Just make sure to wait to append until you want the code to execute.
I will try it here, I get back in a min to mark the answers
Got it; it is not quite the same you said, but is the same logic. After using write to put the html on the frame I did: $("#frame").contents().find('body').append(js);
still have a problem, the solution is good in timing, but it is executing out of context: when it runs it runs in the main page context. Like if I do: $('input').css('background-color','#fff'); it will get inputs from the main page and not from the Iframe.
|
1

Script tags won't execute if their [type] attribute is set to anything wacky.

<script type="wacky/non-executing">
  console.log("This will not execute! You will not see this!");
</script>

3 Comments

Doesn't answer question nor is the type set to anything weird in example
@EnjoysTurtles: Q: "Append <script> when preparing a html code for iframe: without executing" A: "Script tags won't execute if their [type] attribute is set to anything wacky" Expanded Answer: You can append a <script>, without executing it, by setting its [type] attribute to anything unrecognized by the browser. [type="text/javascript"] will cause execution, as will the attribute's omission from the script tag. This means you could keep a script laying dormant in HTML code, and trigger execution by setting the script tag's [type] to "text/javascript".
@EnjoysTurtles: Something like $myHTML.find('script[type="your-mother"]').attr({type:"text/javascript"}); could be used to execute dormant scripts.
1

Its Obvious to run script when you insert it, between script tags,Because your DOM already complete loads. And it will run twice because you put it inside the body So when you body content canges the script ran again!

So you have to set your Script tag on head of an iframe to it will run Only when you insert it or reload,and not again an again !

I am not suggesting you to use eval() because it is dangerous to use for script evaluation,eval() is basically used for another purpose !

Use <script></script> tags to run your script and place it on head if you don't want to ran it twice .

var script = document.createElement('script');
script.innerHTML =  $("#EJs").val();
iframe.contentWindow.document.head.appendChild(script);

May be this will help you..

Comments

0

Try using entities, like

var encodeHtmlEntity = function(str) {
  var buf = [];
  for (var i=str.length-1;i>=0;i--) {
    buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''));
  }
  return buf.join('');
};
html.find('body').append(encodeHtmlEntity(js));

to append and

var decodeHtmlEntity = function(str) {
  return str.replace(/&#(\d+);/g, function(match, dec) {
    return String.fromCharCode(dec);
  });
};
decodeHtmlEntity(html.find('body').val());

to read.

Comments

0

Got it in the way I need. I was trying to add the script tag without using ways around the problem - I mean I just wanted to add the tag as it is. Thanks every body for the inputs - I got the solution from your advises... in the end I could append to the Iframe but it was executing in the main page context. What was causing the problem was using JQuery to do the appending... so here it is:

frame = document.getElementById("frame");
out = (frame.contentWindow) ? frame.contentWindow : (frame.contentDocument.document) ? frame.contentDocument.document : frame.contentDocument;

out.document.open();
out.document.write(html.find('html')[0].outerHTML);//HTML added here

    //JS appended here
js=out.document.createElement('script');
js.innerHTML = 'js code here';
out.document.getElementsByTagName("body")[0].appendChild(js);


out.document.close();

I hope its useful for someone...

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.