Can you use hash navigation without affecting history?

JavascriptJqueryBrowser HistoryFragment IdentifierBrowser State

Javascript Problem Overview


I'm afraid it might be https://stackoverflow.com/questions/1263509/potential-problems-setting-window-location-hash#comment1098554_1265461">impossible</a> but is there a way to change the hash value of a URL without leaving an entry in the browser's history and without reloading? Or do the equivalent?

As far as specifics go, I was developing some basic hash navigation along the lines of:

//hash nav -- works with js-tabs
var getHash = window.location.hash;
var hashPref = "tab-";
function useHash(newHash) {
	//set js-tab according to hash
	newHash = newHash.replace('#'+hashPref, '');
	$("#tabs li a[href='"+ newHash +"']").click();
}
function setHash(newHash) {
	//set hash according to js-tab
	window.location.hash = hashPref + newHash;

	//THIS IS WHERE I would like to REPLACE the location.hash
	//without a history entry

}
	// ... a lot of irrelavent tabs js and then....

	//make tabs work
	$("#tabs.js-tabs a").live("click", function() {
		var showMe = $(this).attr("href");
		$(showMe).show();
		setHash(showMe);
		return false;
	});
	//hash nav on ready .. if hash exists, execute
	if ( getHash ){
		useHash(getHash);
	}

Using jQuery, obviously. The idea is that in this specific instance 1) making the user go back over every tab change could effectively 'break the back button' by piling up needless references, and 2) not retaining which tab they're currently on if they hit refresh is an annoyance.

Javascript Solutions


Solution 1 - Javascript

location.replace("#hash_value_here"); worked fine for me until I found that it doesn't work on IOS Chrome. In which case, use:

history.replaceState(undefined, undefined, "#hash_value")

> history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.

Remember to keep the # or the last part of the url will be altered.

Solution 2 - Javascript

location.replace("#hash_value_here"); 

The above seems to do what you're after.

Solution 3 - Javascript

Edit: It's been a couple years now, and browsers have evolved.

@Luxiyalu's answer is the way to go

--Old Answer--

I too think it is impossible (at this time). But why do you need to change the hash value if you are not going to use it?

I believe the main reason why we use the hash value as programmers is to let the user bookmark our pages, or to save a state in the browser history. If you don't want to do any of this, then just save the state in a variable, and work from there.

I think that the reason to use a hash is to work with a value that is out of our control. If you don't need it, then it probably means you have everything under your control, so just store the state in a variable and work with it. (I like repeating myself)

I hope this helps you out. Maybe there's an easier solution to your problem.

UPDATE: How about this:

  1. Setup a first hash, and make sure it gets saved in the browser history.
  2. When a new tab gets selected, do window.history.back(1), that will make the history go back from your first init hash.
  3. Now you set the new hash, therefore the tabbing will only make one entry in the history.

You'll probably have to use some flags, to know if the current entry can be "deleted" by going back, or if you just skip the first step. And to make sure, that your loading method for the "hash" doesn't execute, when you force the history.back.

Solution 4 - Javascript

You can always create an event listener to catch click events on the hyperlink and in the callback function put e.preventDefault(), that should prevent the browser from inserting it into the history.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser241244View Question on Stackoverflow
Solution 1 - JavascriptLuciaView Answer on Stackoverflow
Solution 2 - JavascriptMattView Answer on Stackoverflow
Solution 3 - JavascriptguzartView Answer on Stackoverflow
Solution 4 - JavascriptAyman FarhatView Answer on Stackoverflow