0

I am doing a web editor, with a textarea that recives the user code and an iframe that shows in real time the html. I have an event that listen on keyup users , this is the code i use to execute javascript on iframe

var iframe = $("#iframeresults").contents()  
$("#wrapper").on("paste keyup", function(){ 

  setTimeout(function(){ 

    iframe.find("body").html(webEditor.getHtml()); 
    iframe.find("style").html(webEditor.getCss()); 

    iframe.find("script").remove() 
    var script = document.createElement("script") 
    script.text = webEditor.getJS() 
    iframe.find("head")[0].appendChild(script) 

  },800) 

}); 

This works fine but i have to remove and create the script tag every time the user press a key, my question is , is there another way to do this without touching the dom?

I tried this link

But if i use eval and the code contains sintax erros, this stops

Community
  • 1
  • 1
Jose Sosa
  • 2,157
  • 3
  • 17
  • 18
  • Can you not add an 'execute' button and execute the script only when that button is pressed? – HBP Sep 19 '13 at 03:40

1 Answers1

0

You can always have the script tag pre-exist, (with an empty 'src' attribute), inside the iframe's imported page, then just augment the tag when you would have normally inserted the entire node. technically your still altering the dom, but since the tag itself lived there from moment of the page's birth, all the mess that your trying to avoid should be easily avoided this way.

  • I did in that way at first but the when i inserted the code in the iframe script tag , the code didn't execute – Jose Sosa Sep 19 '13 at 14:42