The correct way to use element.addEventListener

I have several JavaScript functions that behave as event listeners for a <object/> that fires custom events. The object in question is a YouTube player with support for the JavaScript API. The documentation provides sample code for adding an event listener:

 function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("myytplayer"); ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); // note: quotes used ----------------------^---------------------^ // note: callback function defined in an arbitrary location } function onytplayerStateChange(newState) { alert("Player new state: " + newState); } 

However, according to addEventListener examples I saw elsewhere do not suggest using quotation marks:

 function onytplayerStateChange(newState) { alert("Player new state: " + newState); } function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("myytplayer"); // note: callback function defined EARLIER ytplayer.addEventListener("onStateChange", onytplayerStateChange); } 

So which method is right? The first appeared in all browsers, but lately I have noticed strange problems, and I am wondering if these problems are related to the way the addEventListener method is called.

+3
source share
1 answer

Since the addEventListener method is actually a method open in Flash Player and not its own addEventListener, it really depends on the implementation of AS3 code inside YTPlayer.

I would go with the documentation and use quotes

+3
source

Source: https://habr.com/ru/post/926071/


All Articles