javascript 为什么我的参数没有传递给已调度的事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19345392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why aren't my parameters getting passed through to a dispatched event?
提问by MintDeparture
Ok so I've set up an event listenr like this...
好的,所以我已经设置了一个这样的事件监听器......
window.addEventListener('message', parseMessage, false);
var parseMessage = function(rawMessage) {
console.log(rawMessage.cmd);
};
And then I'm triggering the event like this:
然后我触发这样的事件:
var event = new Event('message', {'cmd':"blerg!"});
window.dispatchEvent(event);
The problem is the console.log in parse message is logging out undefined when I am expecting to to log out "blerg!"
问题是解析消息中的 console.log 正在注销 undefined 当我希望注销“blerg!”
What I am I doing wrong here with the events, how to I pass the 'cmd' message through to the event?
我在事件中做错了什么,如何将“cmd”消息传递给事件?
回答by ComFreek
Use
CustomEvent
insteadofEvent
for creating custom events.Specify your data in a 'details' object (see code).
I changed the event name because
message
is also used for the postMessage API. It didn't cause problems when running in Chrome, but I wouldn't use it.
使用替代的用于创建自定义事件。
CustomEvent
Event
在“详细信息”对象中指定您的数据(请参阅代码)。
我更改了事件名称,因为
message
它也用于postMessage API。在 Chrome 中运行时它没有引起问题,但我不会使用它。
var parseMessage = function(rawMessage) {
console.log(rawMessage);
console.log(rawMessage.detail.cmd);
};
// changed event name
window.addEventListener('myMessage', parseMessage, false);
// data should be in a 'details' object
var evt = new CustomEvent('myMessage', {
detail: {
'cmd' : "blerg!"
}
});
window.dispatchEvent(evt);
Here is an adjustment for IE >= 9 compatiblity (using document.createEvent()
and CustomEvent::initCustomEvent()
):
这是对 IE >= 9 兼容性的调整(使用document.createEvent()
和CustomEvent::initCustomEvent()
):
var evt = document.createEvent("CustomEvent");
evt.initCustomEvent('myMessage', false, false, {
'cmd': "blerg!"
});
回答by olefrank
For IE9/10 polyfill you can use this code provided by Mozilla:
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
对于 IE9/10 polyfill,您可以使用 Mozilla 提供的此代码:https:
//developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
(function () {
if (
typeof window.CustomEvent === "function" ||
// In Safari, typeof CustomEvent == 'object' but it otherwise works fine
this.CustomEvent.toString().indexOf('CustomEventConstructor')>-1
) { return; }
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
})();
Also described here but with wrong URL: https://stackoverflow.com/a/22946340/1736012
此处也有描述,但 URL 错误:https: //stackoverflow.com/a/22946340/1736012