English 中文(简体)
Why does this XUL code works and the Javascript equivalent doesn t?
原标题:

xul way:

<toolbar id="PersonalToolbar">
  <toolbarbutton 
    id="Testing-Doit-Button2"
    class="bookmark-item pagerank"
    tooltiptext="Do it!"
    oncommand="testing_doit();" 
  />
</toolbar>

javascript way:

function createBookmarkItem() {
    const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
    var item = document.createElementNS(XUL_NS, "toolbarbutton");
    item.setAttribute("id", "Testing-Doit-Button2");
    item.setAttribute("class", "bookmark-item pagerank");
    item.setAttribute("tooltiptext", "Do it!");
    item.setAttribute("oncommand", "testing_doit();");
    return item;
}
function placeBookmarkItem() {
 var toolbar = document.getElementById("PersonalToolbar");
 var button = createBookmarkItem();
 toolbar.appendChild(button);
}
placeBookmarkItem();

The xul way shows a button. The Javascript way shows a button when I go to the Javascript Shell of the Extension Developer s Extension, then click enumerateWindows(), then click chrome://browser/content/browser.xul, then type the code, then hit enter. The Javascript way doesn t show a button when I include button.js in button.xul. Why?

最佳回答

A part of the code that I didn t quote here and that had an error made not work the part of the code that I quoted here and that didn t have an error. Take a look at "I add 10 functions to a code, I don’t even call any of them, but the code stops working!" for more information.

问题回答

You need to place the placeBookmarkItem(); inside the documents onload event. The javascript gets executed before the document is in place. Otherwise place the <script/> element just before the </window> element in the end of the document.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签