简体   繁体   中英

Load external javascript file where document.write is used

i'm trying add external javascript code with this code:

function get_js(b){
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = b;
head.appendChild(script);
    }

This external javascript code using document.write inside:

document.write('<script language="JavaScript">document.write(\'<div id="something" style="position: fixed; top: 0; bottom: 0; left: 0; right: 0;">&nbsp;<\/div>\');...

When i'm trying execute function from this file, i'm getting this error:

ReferenceError: some_function is not defined

I thought that document.write in JS code is problem. Can anybody help me?

Thanks in advance!

///

OK, figured it out with POSTSCRIBE: https://github.com/krux/postscribe

don't use document.write() . If you call document.write() after the page is load, it will "reopen" the pageload and as a sideeffect clear everything from the screen.

take a look at properties/methods like innerHTML , textContent , document.createElement() , appendChild() , ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM