Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making browsers ignore the URL hash when the back button is clicked

For example, if an user is on http://example.com, then the user goes to http://example.com#comments. If the user clicks "back" on his browser, how can I make him "ignore" http://example.com and go directly to the URL that he visited before that?

I have jQuery loaded.

like image 964
Leo Jiang Avatar asked Feb 05 '12 02:02

Leo Jiang


People also ask

Can I disable browser back button?

You can-not actually disable the browser back button. However, you can do magic using your logic to prevent the user from navigating back which will create an impression like it is disabled.

How do I disable back forward and refresh functionality in browser?

function disableBackButton() { window. history. forward(); } setTimeout("disableBackButton()", 0);

How do I disable the Back button in Chrome?

To disable the browser "Back" button for a specific page: Open the "UI" tab of the page description window ( "Description" option in the page popup menu). In the "Options" area, for the option "Browser "Back" button", select "Forbidden".

How do I prevent someone from going back to previous page?

function preventBack() { window. history. forward(); } setTimeout("preventBack()", 0); window.


1 Answers

Instead of having a link like:

<a href='#comments'>Link</a>

Use location.replace() to "overwrite" the record of http://example.com in the browser's history.

https://developer.mozilla.org/en/DOM/window.location

Example:

HTML:

<a id='commentsLink'>Link</a>

JavaScript:

$("#commentsLink").click(function(){
    window.location.replace("#comments");
});
like image 166
Chris Laplante Avatar answered Nov 15 '22 07:11

Chris Laplante