Monday, May 20, 2024
59
rated 0 times [  62] [ 3]  / answers: 1 / hits: 135826  / 15 Years ago, sun, november 8, 2009, 12:00:00
document.getElementById('container').addEventListener('copy',beforecopy,false );

In Chrome / Safari, the above will run the "beforecopy" function when the content on the page is being copied. MSIE is supposed to support this functionality as well, but for some reason I'm getting this error:



"Object doesn't support this property or method"



Now, it's my understanding that Internet Explorer won't play with the body node, but I would have thought providing a node by ID would work fine. Does anyone have any ideas about what I'm doing wrong?


** Bonus points for anyone who can tell me what the 3rd parameter "False" is good for.


More From » internet-explorer

 Answers
26

In IE you have to use attachEvent rather than the standard addEventListener.



A common practice is to check if the addEventListener method is available and use it, otherwise use attachEvent:



if (el.addEventListener){
el.addEventListener('click', modifyText, false);
} else if (el.attachEvent){
el.attachEvent('onclick', modifyText);
}


You can make a function to do it:



function bindEvent(el, eventName, eventHandler) {
if (el.addEventListener){
el.addEventListener(eventName, eventHandler, false);
} else if (el.attachEvent){
el.attachEvent('on'+eventName, eventHandler);
}
}
// ...
bindEvent(document.getElementById('myElement'), 'click', function () {
alert('element clicked');
});


You can run an example of the above code here.



The third argument of addEventListener is useCapture; if true, it indicates that the user wishes to initiate event capturing.


[#98363] Wednesday, November 4, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
cadendericki questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Wed, Jul 8, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;