I am working with iframe tag for displaying ads.

I am using below code for it -

<iframe></iframe> 

JS -

var iframe = document.getElementById('abc'); var doc = iframe.contentDocument ? iframe.contentDocument :(iframe.contentWindow ? iframe.contentWindow.document : iframe.document) doc.open(); doc.write('<scr'+'ipt src=""></scr' + 'ipt>'); doc.close(); 

I tried it on normal HTML page and body gets blank in inner document of Iframe, but same code I execute in codepen it get executed, and body filled with executed script.

I needed solution or alternate solution to make it work.

Codepen result

Codepen Inspect

Local inspect

enter image description here

1 Answer

Try oldskool

DEMO

<iframe name="iframe" src="emptypage.html"></iframe> 

JS -

window.onload=function() { var iframe = window.frames['iframe']; var doc = iframe.document; doc.write('<scr'+'ipt src=""></scr' + 'ipt>'); doc.close(); } 

Here is what I see now I add the script to another iframe raw and turn off ad blocking so it seems to work.

enter image description here

23

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy