How to setup javascript redirect to send user to back page? see code

Viewed 65839

With onclick="history.go(-1);return false;" user can navigate to back pages. But if I want to put a confirm method before it takes user to the back. How would I do that?

I think it can be achieved by doing something like below but I am not sure how to redirect user to back page?

$('.clickme').click(function() {
    if(confirm("Are you sure you want to navigate away from this page?"))
    {
      //window.close();
      // how to redirect to history.go(-1) ??
    }        
    return false;
 });

UPDATE

Also is there any way I can check if history has some values in it or is empty? So that I can alert user if is empty?

5 Answers

It's quite as simple as you think it is:

$('.clickme').click(function() {
   if(confirm("Are you sure you want to navigate away from this page?"))
   {
      history.go(-1);
   }        
   return false;
});

Try this:

    if(confirm("Are you sure you want to navigate away from this page?"))
    {
      history.back();
    }     
$('.clickme').click(function() { 
        if(history.length != 0)
        {
            if(confirm("Are you sure you want to navigate away from this page?")) 
            { 
                history.go(-1);
            }
         return false;
        }
        alert("No history found");
        return false;
});
Related