[英]Change syntax event listener in Javascript
我在 Javascript 中编写了一些代码,需要进行一些转换,但我不知道如何实现。
我的代码:
gBrowser.addEventListener("DOMContentLoaded", function(e) {...}), false);
进入
window.addEventListener("load", function() {myExtension.init()}, false);
这里的关键是传递参数 e。 我试过“function(e)”和“myExtension.init(e)”,但它不起作用。
编辑(我的所有代码 - function 代码位于代码末尾):
var myExt_urlBarListener = {
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onLocationChange: function(aProgress, aRequest, aURI)
{
myExtension.processNewURL(aURI);
},
onStateChange: function(a, b, c, d) {},
onProgressChange: function(a, b, c, d, e, f) {},
onStatusChange: function(a, b, c, d) {},
onSecurityChange: function(a, b, c) {}
};
var myExtension = {
oldURL: null,
init: function(e) {
// Listen for webpage loads
gBrowser.addProgressListener(myExt_urlBarListener,
Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
},
uninit: function(e) {
gBrowser.removeProgressListener(myExt_urlBarListener);
},
processNewURL: function(aURI) {
if (aURI.spec == this.oldURL)
return;
// now we know the url is new...
this.oldURL = aURI.spec;
var href = aURI.spec;
var vars = [], hash;
var hashes = href.slice(href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
if(vars[0].match(/^http:\/\/www\.google\.fr/))
{
//I want use the event e here
//But it doesn't work
displayBookmarks(e);
}
}
};
//The following doesn't work (I get "e is not defined" in the Error Console)
window.addEventListener("DOMContentLoaded", function(e) {myExtension.init(e)}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);
不要忘记在 function 声明中添加参数:
window.addEventListener("load", function(e) {myExtension.init(e)}, false);
...
init: function(e) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.