简体   繁体   English

新手在Firefox扩展(XUL)的Javascript中停留在基本事件侦听器上

[英]Newbie stuck on basic event listeners in Javascript for Firefox extension (XUL)

I've been working through the MDN tutorials, but I haven't been able to get JS event listeners to work yet. 我一直在研究MDN教程,但是还无法使JS事件侦听器正常工作。 I'm up to the last bit of this page: https://developer.mozilla.org/en-US/docs/XUL/Tutorial/Adding_Event_Handlers 我到此页面的最后一点: https : //developer.mozilla.org/zh-CN/docs/XUL/Tutorial/Adding_Event_Handlers

In XUL I have a menuitem: menuitem id="appmenu-libraryHistory" label="History"/> 在XUL中,我有一个menuitem:menuitem id =“ appmenu-libraryHistory” label =“ History” />

And in JS I have this: var HistoryWatch = document.getElementById("appmenu-libraryHistory"); 在JS中,我有:var HistoryWatch = document.getElementById(“ appmenu-libraryHistory”); HistoryWatch.addEventListener('command', showLibrary, "History"); HistoryWatch.addEventListener('command',showLibrary,“ History”); function showLibrary(aLeftPane) { ... } 函数showLibrary(aLeftPane){...}

I thought this would pass the string "History" to the function showLibrary when the menu entry "History" was pressed. 我以为这会在按下菜单项“历史记录”时将字符串“历史记录”传递给函数showLibrary。 The showLibrary function does work if I pass parameters directly from the XUL, but if I do that it won't let me pass a URI which is what I'll want to do eventually. 如果我直接从XUL传递参数,则showLibrary函数确实可以工作,但是如果这样做,则不会让我传递URI,这是我最终要执行的操作。

I suspect the JavaScript you have shown is in a file that is included as part of a <script> tag. 我怀疑你已经证明了JavaScript是在包括作为的一部分文件<script>标记。 It may not work because by the time the document.getElementById() line runs, the page hasn't been constructed yet, and there is no element with this id yet. 它可能不起作用,因为到document.getElementById()行运行时,页面尚未构建,并且尚无具有此ID的元素。

You need to wait for the page to fully load to be sure that your menu exists. 您需要等待页面完全加载,以确保菜单存在。 If you are working on a separate window you can call an initialization function from its onload attribute in xul. 如果在单独的窗口上工作,则可以从xul中的onload属性调用初始化函数。 If you are overlaying just a menu in an existing window, you will need to listen to the load event of the parent window and call your initialization from there : 如果仅在现有窗口中覆盖菜单,则需要侦听父窗口的load事件并从那里调用初始化:

function init()
{
  window.removeEventListener("load", init, false); // Remove the handler as we don't need it anymore.

  var historyWatch = document.getElementById("appmenu-libraryHistory");
  historyWatch.addEventListener('command', showLibrary, "History");
}

window.addEventListener("load", init, false);

Note that in this example, init (and showLibrary ) are polluting the global namespace, it would be better to encapsulate them in an object. 请注意,在此示例中, init (和showLibrary )正在污染全局名称空间,最好将它们封装在一个对象中。

Also note that the order matters because when the last line runs, the init function must have been declared already, otherwise it will attach nothing ( undefined ). 另请注意,顺序很重要,因为在最后一行运行时,必须已经声明了init函数,否则它将不附加任何内容( undefined )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM