below I have some JavaScript which listens for mouse activity and starts a timer if there is no activity and then redirects the user to another page after say 2 minutes..The problem is that if I have this in a layout page it works fine but when I put it in the content editor part in sharepoint the timer works but the page does not redirect..Can someone please tell me what's wrong.

<script type="text/javascript">

    var mins, secs, TimerRunning, TimerID;
    TimerRunning = false;

    var activity;
    document.documentElement.onmousemove = function () {
         activity = Init();
       // activity = setInterval(saySomething, 5000);

    function Init() //call the Init function when u need to start the timer
        mins = 2;
        secs = 0;

    function StopTimer() {
        if (TimerRunning)
        TimerRunning = false;

    function StartTimer() {
        TimerRunning = true;
        window.status = "if no activity is detected you will be logged out in " + Pad(mins) + ":" + Pad(secs);
        TimerID = self.setTimeout("StartTimer()", 1000);


        if (mins == 0 && secs == 0)

        if (secs == 0) {
            secs = 60;


    function Check() {
        if (mins == 1 && secs == 0)

        // alert("You have only 2 minutes remaining");
        if (mins == 0 && secs == 0) {

            window.location = "Http://Test";


    function Pad(number) //pads the mins/secs with a 0 if its less than 10
        if (number < 10)
            number = number;
        return number;

    } </script>
  • 2
    try window.location.href? That always worked for me. Feb 21, 2012 at 4:04
  • I replaced windows.location with windows.location.href but it is not working..the timer stops but it does not redirect
    – steve
    Feb 21, 2012 at 4:25
  • 3
    did you place an alert message inside the if loop and check whether the condition is being called, just before the window.location ?
    – Deepu Nair
    Feb 21, 2012 at 4:48
  • Yea it wasn't getting inside..forgot to add Curly brackets after first if statement..its workig now....Thanks for helping me.
    – steve
    Feb 21, 2012 at 5:06
  • 2
    Try to show alert box using this way: setTimeout(function(){alert("hi");}, 0); Feb 21, 2012 at 8:45

3 Answers 3


Well, although this is a trick, there is one more thing you can try. Well there is not activity on page for 2 minute, then make the target page link visible on the page and trigger the click action on that link in the page.

For this purpose you can have a look at jQuery trigger function.


Good to know that your issue is solved by adding curly brackets for your if loop.

I believe the setTimeout function commented by Kai would only fire the alert waiting for a particular interval specified. I think thats not what you are intending to do.

You can think about alerting the user before redirecting, by displaying a label text. The label text can be hidden using a div tag and be displayed just within your logic whenever required.

Good Luck Steve.


You can try this one,

window.open("http://www.w3schools.com");//opens in new tab


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.