2011-05-26 184 views
2

我想通過我的Firefox插件加載到第三方網頁的外部JavaScript(jQuery),WebDeveloper插件顯示jquery腳本語法成功地附加到「查看生成的源」的頭部分,但腳本沒有得到執行/取出?,我在我的插件中使用以下代碼加載腳本:在頁面加載後加載並執行腳本?

function loadjscssfile(filename, filetype){ 
if (filetype=="js"){ //if filename is a external JavaScript file 
    var fileref=document.createElement('script') 
    fileref.setAttribute("type","text/javascript") 
    fileref.setAttribute("src", filename) 
    fileref.setAttribute('onload', 'firefoxInit()'); 

} 
else if (filetype=="css"){ //if filename is an external CSS file 
    var fileref=document.createElement("link") 
    fileref.setAttribute("rel", "stylesheet") 
    fileref.setAttribute("type", "text/css") 
    fileref.setAttribute("href", filename) 
} 
if (typeof fileref!="undefined") 
    document.getElementsByTagName("head")[0].appendChild(fileref) 
} 

請求。指導如何前進。

+0

你不會在最後的if語句中獲得'fileref',因爲它在第一個'if-else'範圍內聲明。但是懷疑這就是你正在尋找的答案;) – 2011-05-26 09:18:22

+1

贊成直接屬性分配超過設置屬性,在某些瀏覽器中的bug,使用fileref.src而不是 – Ibu 2011-05-26 09:18:49

+0

@freaktm - 奇怪的腳本仍然被附加到DOM? – Stacked 2011-05-26 15:54:28

回答

0

您可以在javascript的末尾添加firefoxInit();並刪除onload屬性分配。

+0

謝謝麥克,我開始與JavaScript,並給了錯誤:「firefoxInit沒有定義」 – Stacked 2011-05-29 11:57:46

+0

我認爲'firefoxInit'在這個JS文件中定義,但可能不是。如果你在JS文件的末尾調用一個函數,你可以確定,之前的所有內容都被加載了。在這個函數調用中,您可以運行該命令來啓動邏輯。 – Mic 2011-05-30 07:35:28