Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 735
user187580 Avatar asked Feb 24 '10 13:02

user187580


People also ask

How do I redirect a back page?

There are two approaches used to redirect the browser window back. Approach 1: Using history. back() Method: The back() method of the window. history object is used to go back to the previous page in the current session history.

How do I redirect a previous page in HTML?

In a web browser, the built-in JavaScript object window has an object called history containing the URLs a user has visited in their current browser window. You can use the history. back() method to tell the browser to go back to the user's previous page.

What function is used to redirect someone to another page?

Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.


2 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; }); 
like image 169
David Hedlund Avatar answered Sep 19 '22 15:09

David Hedlund


Try this:

    if(confirm("Are you sure you want to navigate away from this page?"))     {       history.back();     }      
like image 29
Sarfraz Avatar answered Sep 18 '22 15:09

Sarfraz