javascript - How can I append and execute a script inside an iframe in a cross browser way (specifically IE!)? -


Background: I need to load ad scripts after loading the dome. Because many scripts use the document. To run writing () and other potentially bad jobs after the DOM loads, I want to load the script inside an iframe.

Therefore, when an advertisement is required to show, an event has started which does the following:

  var iframe = document.createElement ('iframe'); Iframe.setAttribute ('id', 'iframeId'); Iframe.setAttribute ('src', 'about: empty'); Var adContainer = document.getElementById ('AdContainer'); AdContainer.appendChild (iframe); Var val = '& lt; Html & gt; & Lt; Head & gt; & Lt; / Head & gt; & Lt; Body & gt; '; Val + = '& lt; Scr '+' ipt type = "text / javascript" src = "'+ url +'" & gt; & Lt; / Scr '+' ipt & gt; '; Val + = '& lt; / Body & gt; & Lt; / Html & gt; ';  

If I do not specify Val and HTML and body tags, then the script automatically gets attached to the top of the IFrame and fails to execute it in FF. In IE, the script does not execute with or without the head / body / HTML tag.

  var doc = iframe.contentWindow || Iframe.contentDocument; If (doc.document) {doc = doc.document} doc.open (); Doc.write (val); Doc.close ();  

I found this last code here:. Although I do not want to load jquery in the iframe, I just want to attach a script and execute it.

My current solution works very well in FF and WebKit. However, the IE script does not execute. The script is written on the page, but does not start to run Is there a better way to add scripts to iPrm, then will it run cross browser? Or is it possible to tell IE to run the script?

I know that I can load an external document with the ad script through the IPL, but I do not want to make additional calls on my server, except if I can do it dynamically. , I tried to use the application child () to insert the script on the body of the iframe, but since the script element is made out of the dome of the iframe, it does not look like is.

In the IE script tag defer = "true" is required when it Must execute dynamically.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -