views:

124

answers:

3

Hello,

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;
 });

Any ideas?

Thank you.

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??

+3  A: 

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;
});
David Hedlund
oh I should have tried it first before asking. let me try.
is there any way to check if there is "something" in history .. i.e. not empty etc??
There exist a history.previous property but as the JSRef states it *requires the UniversalBrowserRead privilege. It has no value if you do not have this privilege. For information on security, see the Client-Side JavaScript Guide.*
anddoutoi
yes, you can check `history.length`. `onclick="if(history.length == 0) { alert("there's nowhere to go!"); } else { if(confirm('are you sure?')) { history.go(-1); } }"`
David Hedlund
Thanks Hedlund.
+1  A: 

Try this:

    if(confirm("Are you sure you want to navigate away from this page?"))
    {
      history.back();
    }     
Sarfraz
can it be put in onclick="return confirm('confirm message')";??
Have you tried it?
Blair McMillan
yes it can be put there, try it out.
Sarfraz
+1  A: 
$('.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;
});
Dodswm