2016-03-02 91 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() ...