Link to home
Start Free TrialLog in
Avatar of vdesai_8
vdesai_8

asked on

Redirect Javascript

Dear Gurus,
I have an application using ASP.NET, VB.Net 2003.
I am using the following javascript to track inactivity(mouse or keyboard movements) on webpage "home.aspx".

It works fine & redirects to google.com if I click on some application other than home.aspx, but if I stay on home.aspx and there is no activity for 2 seconds it does not redirect to google.com(which is what it is supposed to do).

<SCRIPT language="JavaScript" type="text/javascript">
      var TimerId = 100;
      
      function ResetTimer() {
        clearTimeout(TimerId);
        TimerId = window.setTimeout("document.location.href='http://www.google.com';",2000);
      }
      
      document.onkeypress = ResetTimer;
      document.onmousemove = ResetTimer;

    </SCRIPT>

Please advise what could be wrong here.

Thanks,
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

write
<body onload= "javascript:ResetTimer();">

this will reset the timer when the page is loaded for the first time and so take care of case when you stay in home.aspx

Rejo
Avatar of hielo
That is because the timeout is withing a function which you never call.

Add window.onload=ResetTimer;
(No Parentheses)
Avatar of vdesai_8
vdesai_8

ASKER

Thanks Gurus.
It works both the ways as mentioned by RejoJohny & Hielo, but not quite the way I want it to.

I would like if there is no user activity(no KB or mouse movements) for 5 seconds the user should be taken to www.google.com otherwise he can continue using the application as long as he is active.

By using both the above solutions the user is redirected to google in 5 seconds irrespective if he is active or not.

Please help.
Thanks.

 
I mixed up the number of seconds of user inactivity. Please forgive. Lets make it x seconds. Thanks!
I tested this and it works as long as there is activity:

<SCRIPT language="JavaScript" type="text/javascript">
      var TimerId = 100;
     
      function ResetTimer() {
        clearTimeout(TimerId);
        TimerId = window.setTimeout("document.location.href='http://www.google.com';",5000);
      }
     
      document.onkeypress = ResetTimer;
      document.onmousemove = ResetTimer;
window.onload=ResetTimer;
    </SCRIPT>
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hielo,
I tried your solution & it works fine but there is a catch.
It works only if the cursor goes out of focus of the page content for more than 5 seconds.
If the user has the cursor on the page & then he walks away for 2 minutes, the html considers it as user activity & doesnt redirect the user to google.com(which I would like it to do).

Please advise.
Thanks for your help.



Well, this is quite a mystery because the full example I sent you works the way you want. I just tested in IE and FF. As long as I type in the textarea, or move the mouse on top of the window I remain on the page. As soon an I stop typing, the cursor remains blinking on the textarea and 5 seconds later, Hello Google. I even tabbed out of textarea and it still works. Perhaps some other javascript code is interfering with the page. Test he exact code as provided on my previous post. Save it to your desktop and then open it via File>Open. Do you have a URL?
What you are describing makes perfect sense if you were using onfocus instead of onmousemove and onkeypress. Are you sure you are not using onfocust. On which browser are you testing it. For me it worked in FF 2+ and IE 6
Hielo,
It worked like a charm at my home computer. not sure why it did not work in office computer. I will check it tomm morning & let you know.

Thanks for your help!

It works great! Thanks Hielo
Always a pleasure. What was the issue with your work machines? Cached pages? conflicting extensions? browser scripting restrinctions?
not sure what was the issue. Maybe some other scripts got mixed up. I created a fresh html page today & copied your solution. It worked perfect:)

Thanks again!
 
Glad to hear things worked out for you.
FYI:
The reason I posted a full/"stand alone" solution was for you to check it as is and verify that it does what you wanted it to do. IF so, then it is just a matter of integrating into your project. If after integrating it into your project it still does not work, that is a sign of a scripting conflict. Most likely there events that are overriden somewhere.  When this this is the case, many times it is faster to just start from scratch and check your results little by little as you are adding more code. Best of luck.