Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possible to replace window.location.hash?

I'm wondering whether it's possible to change the hash in window.location.hash and replace it with 'this.id'. Or would I need to change the entire window.location?

like image 953
circey Avatar asked Jul 31 '10 06:07

circey


People also ask

What is hash in window location?

The hash property of the Location interface returns a string containing a '#' followed by the fragment identifier of the URL — the ID on the page that the URL is trying to target. The fragment is not percent-decoded. If the URL does not have a fragment identifier, this property contains an empty string, "" .

What does Window location replace do?

Window location. The replace() method replaces the current document with a new one.

How do you remove hash?

To remove the hash URL, you can use the replaceState method on the history API to remove the hash location. Example: HTML.

How do I change the URL of a hash?

hash = '#food'; This will replace the URL's hash with the value you set for it. If you wish the change the hash within the URL when the user clicks an anchor tag, why not just use <a href="#bar">Foo</a> ?


1 Answers

Yes, you can. I do something similar at one of my sites, although with href instead of id, but id works too. A quick example:

$('a[id]').click(function(e) {     // This will change the URL fragment. The change is reflected     // on your browser's address bar as well     window.location.hash = this.id;     e.preventDefault(); }); 
like image 66
BoltClock Avatar answered Sep 16 '22 14:09

BoltClock