0

I wrote a script which redirects user on page refresh. I got it working on newer versions of Firefox, chrome and Internet explorer. However, most of my users still using older version of browsers and this script is not working on those. Which approaching I should do to get it working ?

var redirecting= false;
window.onbeforeunload = function() {
    if (redirecting) return;

        setTimeout(function() {

            window.top.location.href= 'http://www.google.com';
   setTimeout(function() {
      redirecting=true;
  }, 1000);
        }, 1);

};
user3304007
  • 446
  • 1
  • 7
  • 17
  • Which browsers and versions do they use? – Oriol Mar 19 '14 at 23:25
  • http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – martynas Mar 19 '14 at 23:25
  • Do you have any information on what error messages they get in their consoles? – Paul S. Mar 19 '14 at 23:27
  • @oriol They mostly use IE 7,8,9,10. Paul, unfortunetly, no. What should I tell them to get error messages in their consoles ? – user3304007 Mar 19 '14 at 23:31
  • @user3304007 Your code works for me on IE8 and IE8 in IE7 mode. Anyway, why do you delay `redirecting=true`? This causes a strange & processor intensive loop... – Oriol Mar 19 '14 at 23:43

2 Answers2

2

use window.location.replace('http://www.google.com')

4m1r
  • 12,234
  • 9
  • 46
  • 58
  • window.top would usually be used from a frame, whereas window.location.replace should work consistently across most browsers. https://developer.mozilla.org/en-US/docs/Web/API/Window.location – 4m1r Mar 19 '14 at 23:54
0

change

window.top.location.href= 'http://www.google.com';

to

top.window.location.href = 'http://www.google.com'; 
Sandeep Sherpur
  • 2,418
  • 25
  • 27