javascript - Cannot call method 'appendChild' of null for iframes -
i have following script create iframe
function createiframe() { var iframe = document.createelement("iframe"); iframe.style.position = "absolute"; iframe.style.visibility = "hidden"; document.body.appendchild(iframe); // firefox, opera if(iframe.contentdocument) iframe.doc = iframe.contentdocument; // internet explorer else if(iframe.contentwindow) iframe.doc = iframe.contentwindow.document; // magic: force creation of body (which null default in ie). // force styles of visited/not-visted links. iframe.doc.open(); iframe.doc.write('<style>'); iframe.doc.write("a{color: #000000; display:none;}"); iframe.doc.write("a:visited {color: #ff0000; display:inline;}"); iframe.doc.write('</style>'); iframe.doc.close(); // return iframe: iframe.doc contains iframe. return iframe; }
however in chrome console giving me error cannot call method 'appendchild' of null
why not working?
the code correct. have added body in page?
try this:
<html> <body> <script> window.onload=function() { createiframe() }; function createiframe() { var iframe = document.createelement("iframe"); iframe.style.position = "absolute"; iframe.style.visibility = "hidden"; document.body.appendchild(iframe); // firefox, opera if(iframe.contentdocument) iframe.doc = iframe.contentdocument; // internet explorer else if(iframe.contentwindow) iframe.doc = iframe.contentwindow.document; // magic: force creation of body (which null default in ie). // force styles of visited/not-visted links. iframe.doc.open(); iframe.doc.write('<style>'); iframe.doc.write("a{color: #000000; display:none;}"); iframe.doc.write("a:visited {color: #ff0000; display:inline;}"); iframe.doc.write('</style>'); iframe.doc.close(); // return iframe: iframe.doc contains iframe. return iframe; } </script> </body> </html>
iframe.doc.write not cleanest solution....
Comments
Post a Comment