document.addEventListener doesnt fire in IE 9 or any other IE versions

Hello,

I have a situation here, we want to copy the contents of grid (written in MVC) to clipboard and then copy it to Excel (using CTRL-V). So when I click on COPYGRID button on UI the following function copyGrid()  is called but the eventlistener (document.addEventListener) doesn't get fired. Your help is highly appreciated as this issue is kind of urgent.


Here is the code
-----------------------
document.addEventListener('copy', function (e) {
  console.log(e);
  try{
  alert("starting to copy to clipboard");
        e.clipboardData.setData('text/html', searchString);
        e.preventDefault();
        alert("Table copied to clipboard");
    } catch (e) {
      alert("Issue copying table to clipboard");
   }
});

function copyGrid() {
    document.execCommand('copy', false, copyGrid);

}


Regards,
Rithesh
Star79Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Star79Author Commented:
Why this question is neglected?
0
Randy DownsOWNERCommented:
Maybe this will help.

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

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');
});

Open in new window

0
Miguel OzSoftware EngineerCommented:
You should a JavaScript framework like JQuery rather than writing your own event handling wrapper.
For example Jquery use on method, to attach event handlers as shown below:
$(document).on('copy', function (e) {
   console.log(e);
   try{
   alert("starting to copy to clipboard");
         e.clipboardData.setData('text/html', searchString);
         e.preventDefault();
         alert("Table copied to clipboard");
     } catch (e) {
       alert("Issue copying table to clipboard");
    }
 });

Open in new window

Note: Randy's answer and SO link provides the explanation regarding IE event attachment difference among IE versions, but I prefer to use JQuery because it fixes the problem for any IE version or other browsers supported by the JQuery library.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.