0
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.

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • 1
    Did you wait an hour after setting this? If you log document.readyState immediately after this call, what does it show as? – argentage Jul 19 '15 at 04:53
  • 2
    Why do you think that code will work? Looks like an error to me. You are passing the timeout ID as the callback to an event listener. Either wrap it in an function, or remove the event listener entirely (your timeout function does not manipulate the DOM anyway). – Alexander O'Mara Jul 19 '15 at 04:53
  • I tested it and it still seems to work for much shorter timeout values. It doesn't really seem to be the sane way to do it, but it should still set the timeout, I think. – argentage Jul 19 '15 at 05:01
  • 1
    @airza It "works", but is *independent* of the DOMContentLoaded event. That is the setTimeout is created *immediately* and the result of that function (an integer) is passed as the value to the listener's callback (which is incorrect, although I don't believe this would cause an exception .. but maybe browser differences?). See http://stackoverflow.com/questions/4120781/settimeout-ignores-timeout-fires-immediately – user2864740 Jul 19 '15 at 05:42

3 Answers3

2

Try:

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

The second arg to addEventListener should be a function.

nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29
0

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);
Dave
  • 8,163
  • 11
  • 67
  • 103
Abraar Arique Diganto
  • 1,215
  • 16
  • 24
-1

IE support attachEvent instead of addEventListener

try this

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