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?
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.
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.
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With