Matej Posted June 8, 2014 Share Posted June 8, 2014 Hello gentlemen. Im trying to understand addEventListener and i came to this script <p id="lol" > hello world </p> var g=document.getElementById("lol");g.addEventListener("click",myfunction(){alert(event.target);},false); but it does not work , i tried it with "true"(altought im not quite suire if i understand when to use false /true). Link to comment Share on other sites More sharing options...
Ingolme Posted June 8, 2014 Share Posted June 8, 2014 The problem is here: myfunction(){alert(event.target);} To declare an anonymous function you use the function keyword. Simply function(){alert(event.target);} Link to comment Share on other sites More sharing options...
davej Posted June 9, 2014 Share Posted June 9, 2014 <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style>#lol{cursor:pointer;color:purple;}</style><script>window.onerror = function(m,u,n){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+n)}</script><script>window.onload = function(){document.getElementById("lol").addEventListener("click",function(e){alert('id: '+ e.target.id +'nelement: '+e.target.tagName +'ninnerHTML: '+e.target.innerHTML);},false);}</script></head><body><p id="lol">hello world</p></body></html> Link to comment Share on other sites More sharing options...
davej Posted June 9, 2014 Share Posted June 9, 2014 I don't seem to be able to replace... window.onerror = function(m,u,n){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+n)} ...with... window.addEventListener("error",function(m,u,n){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+n)},false); Link to comment Share on other sites More sharing options...
Ingolme Posted June 9, 2014 Share Posted June 9, 2014 Are the error arguments standard? Under the W3C model event handlers should take just one parameter that holds data about the event. This possibly also applies to the error event. Link to comment Share on other sites More sharing options...
Don E Posted June 9, 2014 Share Posted June 9, 2014 It appears that window.onerror gives information that is not provided by window.addEventListener... as explained in the following link for firefox. For webkit browsers however, seems to work: http://stackoverflow.com/questions/12746034/how-to-get-error-event-details-in-firefox-using-addeventlistener Link to comment Share on other sites More sharing options...
Matej Posted June 9, 2014 Author Share Posted June 9, 2014 thanks , is there any difference between function(e){alert(e.target);} and function(){alert(event.target);} there is written the second case in documentation , but it does not work , but the first does. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 9, 2014 Share Posted June 9, 2014 Different browsers support different models, you would need to check if an event object was passed to the function and, if not, get it from the global variable. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now