Alexander Kao
Alexander Kao

Reputation: 11

Why is this javascript code not working on IE 11?

document.addEventListener("DOMContentLoaded", setTimeout(function(){
    window.location.assign("http://www.google.com")
}, 3600000));

I suspect there is something about IE or IE 11, I need to know about because it works on MF, GC, and Safari.

I thought I read somewhere this should work for IE 9 and above!

In case people are wondering, I am trying to find a simple way to redirect people out of a website, if they have been inactive for too long.

Upvotes: 0

Views: 1667

Answers (3)

kittenchief
kittenchief

Reputation: 1255

I think the problem is that you're passing a setTimeout with a addEventListener. The addEventListener takes a function not a setTimeout. So the solution is to warp it up in a function .

Wraping up in a function:

document.addEventListener("DOMContentLoaded", function(){
  setTimeout(function(){ 
    window.location.assign("http://www.google.com") }, 3600000);
  }
);

IE 9 and up support the addEventListener, but IE 8 and down go with the attachEvent. So the code above should work just fine in your IE 11. But just for completeness:

document.attachEvent("DOMContentLoaded", function(){
  setTimeout(function(){ 
    window.location.assign("http://www.google.com") }, 3600000);
  }
);

Also, because you're code doesn't manipulate the DOM anyway, you can just do:

setTimeout(function(){ 
    window.location.assign("http://www.google.com") }, 3600000);

Upvotes: 0

youssef mohamed
youssef mohamed

Reputation: 45

IE support attachEvent instead of addEventListener

try this

document.attachEvent("DOMContentLoaded", function(){
  setTimeout(function(){ 
    window.location.assign("http://www.google.com") }, 3600000);
  }
);

Upvotes: -1

typically
typically

Reputation: 3570

Try:

document.addEventListener("DOMContentLoaded", function() {
  setTimeout(function(){ window.location.assign("http://www.google.com"); }, 3600000);
});

The second arg to addEventListener should be a function.

Upvotes: 2

Related Questions