2010-09-13 67 views
3

我是TryAgain的Firefox開發者之一,它在網站加載失敗時顯示自定義錯誤頁面。它本質上取代了Firefox的netError.xhtml自定義版本。XUL按鈕不出現

但是,我遇到了3.0 * .3.6。*和Fx4b5之間的一些相當的終端兼容性問題。 (在netError.dtd的條目已更名,導致任何一個版本或其他一個XML解析錯誤。)

爲了解決這個問題,我已經決定要具有擴展動態修改頁面,反對完全替換它。我需要在Fx3中添加到netError.xhtml的一個元素是<xul:button>。然而,用下面的代碼添加它,沒有出現在屏幕上:

var div = document.getElementById("errorContent"); 
var btn = document.createElement("xul:button"); 
btn.setAttribute("label", "Hello world"); 
btn.setAttribute("oncommand", "alert('Hello world!');"); 
div.appendChild(btn); 

我看到在Mozilla開發者中心是there is this note

Gecko implementation of createElement doesn't conform to the DOM spec for XUL and XHTML documents: localName and namespaceURI are not set to null on the created element. See bug 280692 for details.

這是什麼繼承權問題,以及哪能解決它?

此外,如何通過JavaScript執行oncommand事件?

+1

你試過用createElementNS(」 http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul「,」xul:button「)? – lithorus 2010-09-14 22:05:30

回答

1

document.createElement()不接受限定名稱。您傳遞的「xul:button」字符串使其創建一個名爲「xul:button」(== localName)的元素,而不是XUL「按鈕」元素。另一方面,當解析XML時,< xul:button>被解析爲限定名稱:解析器搜索與xul前綴對應的名稱空間(從其中一個父元素中的xmlns:xul=""定義),並創建「按鈕「元素找到它的名字空間。

關於不符合XUL和(X)HTML的DOM規範的說明意味着您可以使用常規document.createElement("buttton")相應地在XUL或HTML文檔的XUL或HTML命名空間中創建元素。

或者你可以去硬盤的方式及用途:

var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; 
document.createElementNS(XUL_NS, "button") 

甚至與合格的名稱,而不是有任何理由這樣做:

document.createElementNS(XUL_NS, "xul:button") 
+0

感謝您的解釋。這是有道理的;我會盡快給它擺動。 – 2010-10-25 22:57:59

+0

果然,這是個訣竅。謝謝! – 2010-11-04 23:51:39

+0

我一直在[接收報告](http://getsatisfaction.com/tryagain/topics/tryagain_error?utm_content=topic_link&utm_medium=email&utm_source=new_topic)該代碼引發以下錯誤:'錯誤:組件返回失敗代碼:0x80040111 (NS_ERROR_NOT_AVAILABLE)[nsIDOMHTMLDocument.createElementNS]'。似乎Firefox偶爾無法訪問命名空間服務器? – 2011-01-30 12:54:05