2016-03-02 104 views
0

我试图添加外部JavaScript代码,此代码:使用document.write内加载外部JavaScript文件,其中文件撰写使用

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); 
    } 

这种外部JavaScript代码:

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

当我想从这个文件执行功能,我得到这个错误:我得到这个错误:

ReferenceError: some_function is not defined 

我以为th在JS代码中的document.write是问题。有谁能够帮助我?

在此先感谢!

///

OK,理解了它与POSTSCRIBE:https://github.com/krux/postscribe

+0

你应该完全避免文件撰写:http://stackoverflow.com/a/802943/3299157 – Ageonix

+0

是的,我知道,但这是第三方脚本。 –

回答

2

不使用document.write()。如果您在加载页面后致电document.write(),它将“重新打开”页面加载并作为副作用清除屏幕上的所有内容。

看看属性/方法,如innerHTMLtextContentdocument.createElement()appendChild() ...