2012-11-17 68 views
0

我讀this tutorial添加CSS文件頁面extansion的init

也就是說CSS文件的加載方式:

var linkTargetFinder = function() { 
    return { 
     init : function() { 
      gBrowser.addEventListener("load", function() {  
       // ... 
       } 
      }, false); 
     }, 

     run : function() { 

      var head = content.document.getElementsByTagName("head")[0], 
      style = content.document.getElementById("link-target-finder-style") 

      if (!style) { 
       style = content.document.createElement("link"); 
       style.id = "link-target-finder-style"; 
       style.type = "text/css"; 
       style.rel = "stylesheet"; 
       style.href = "chrome://linktargetfinder/skin/skin.css"; 
       head.appendChild(style); 
      } 
      // ... 
     } 
    }; 
}(); 
window.addEventListener("load", linkTargetFinder.init, false); 

我不明白爲什麼CSS文件不能被注入到頁面在init

var linkTargetFinder = function() { 
    return { 
     init : function() { 
      gBrowser.addEventListener("load", function() { 
       var head = content.document.getElementsByTagName("head")[0] 

       style = content.document.createElement("link"); 
       style.id = "link-target-finder-style"; 
       style.type = "text/css"; 
       style.rel = "stylesheet"; 
       style.href = "chrome://linktargetfinder/skin/skin.css"; 
       head.appendChild(style); 

       // ... 
       } 
      }, false); 
     }, 

     run : function() { 
      // ... 
     } 
    }; 
}(); 
window.addEventListener("load", linkTargetFinder.init, false); 

樣式在這種情況下不起作用。
爲什麼不能把它放在init

+0

如果您查看錯誤控制檯,您可能會發現有關未定義或空值的錯誤消息。 – paa

回答

0

看起來合理,但對於gBrowser事件使用DOMContentLoaded而不是load

相關問題