Monday, June 3, 2024
59
rated 0 times [  64] [ 5]  / answers: 1 / hits: 17658  / 11 Years ago, fri, april 12, 2013, 12:00:00

I am following a tutorial on Lynda.com about the new DOM event model.


This is the code I am working with.


function addEventHandler(oNode, sEvt, fFunc, bCapture){

if (typeof (window.event) != "undefined")
oNode.attachEvent("on" + sEvt, fFunc);
else
oNode.addEventListener(sEvt, fFunc, bCapture);
}

function onLinkClicked(e){
alert('You clicked the link');
}

function setUpClickHandler(){
addEventHandler(document.getElementById("clickLink"), "click", onLinkClicked, false);
}


addEventHandler(window, "load", setUpClickHandler, false);

I am adding it to the click event on this link


<a href="#" title="click me" id="clickLink">Click Me!</a>

It works perfectly fine in IE, Firefox, Opera but not in Chrome. I've looked around, but have not been able to find anything specific yet. Some similar questions but it does not answer my question.


I get the following error from the Chrome console:



Uncaught TypeError: Object [object HTMLAnchorElement] has no method 'attachEvent'



Any suggestions or a link to the answer?


More From » google-chrome

 Answers
7

Why are you testing:



if (typeof (window.event) != undefined)


...in order to decide whether to use attachEvent()? Chrome does define window.event, so then your code tries to use attachEvent() which is not defined.



Try instead testing for the method directly:



if (oNode.attachEvent)
oNode.attachEvent(on + sEvt, fFunc);
else
oNode.addEventListener(sEvt, fFunc, bCapture);

[#78956] Wednesday, April 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;