简体   繁体   中英

Javascript event custom property disappears

I have an iFrame inside my document, which at one point triggers a custom event:

function notifyEnd(gameStatus) {
    var fireOnThis = parent.document;
    var evObj = document.createEvent('Event');
    evObj.gameStatus = gameStatus;
    evObj.initEvent('gameEnd', true, true);
    fireOnThis.dispatchEvent(evObj);
}

Please note the gameStatus property set on the event.

My document registers an event listener for this event:

document.addEventListener('gameEnd', function(e) {
    document.getElementById("gameEndForm:gameStatus").value = e.gameStatus;
    document.getElementById("gameEndForm:gameCompleteButton").click();
}, true);

However, when the event is fired, the "gameStatus" property has an "undefined" value. I couldn't find any clue as to what could cause such a behaviour.

Thanks for your help,
Sébastien

Seems to me that you want to use a MessageEvent and use the data parameter of initMessageEvent.

http://help.dottoro.com/ljknkjqd.php

You can use JSON is you want to pass more than just one string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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