Getting URL hash location, and using it in jQuery

Editor’s note: the approach below has serious security implications and, depending upon the version of jQuery you are using, may expose your users to XSS attacks. For more detail, see the discussion of the possible attack in the comments on this answer or this explanation on Security Stack Exchange. You can use the location.hash property … Read more

URL Fragment and 302 redirects

Update 2022-09-22: RFC 9110/STD 97 HTTP Semantics (which obsoletes 7231 (and others)), has been published as an INTERNET STANDARD in June 2022. The wording in the newly numbered Section 10.2.2 Location stays the same as before/below. Update 2014-Jun-27: RFC 7231, Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content, has been published as a PROPOSED STANDARD. From … Read more

Modifying location.hash without page scrolling

Use history.replaceState or history.pushState* to change the hash. This will not trigger the jump to the associated element. Example $(document).on(‘click’, ‘a[href^=#]’, function(event) { event.preventDefault(); history.pushState({}, ”, this.href); }); Demo on JSFiddle * If you want history forward and backward support History behaviour If you are using history.pushState and you don’t want page scrolling when the … Read more

How to detect if URL has changed after hash in JavaScript

I wanted to be able to add locationchange event listeners. After the modification below, we’ll be able to do it, like this window.addEventListener(‘locationchange’, function () { console.log(‘location changed!’); }); In contrast, window.addEventListener(‘hashchange’,() => {}) would only fire if the part after a hashtag in a url changes, and window.addEventListener(‘popstate’,() => {}) doesn’t always work. This … Read more

Change the URL in the browser without loading the new page using JavaScript

If you want it to work in browsers that don’t support history.pushState and history.popState yet, the “old” way is to set the fragment identifier, which won’t cause a page reload. The basic idea is to set the window.location.hash property to a value that contains whatever state information you need, then either use the window.onhashchange event, … Read more

How to remove the hash from window.location (URL) with JavaScript without page refresh?

Solving this problem is much more within reach nowadays. The HTML5 History API allows us to manipulate the location bar to display any URL within the current domain. function removeHash () { history.pushState(“”, document.title, window.location.pathname + window.location.search); } Working demo: http://jsfiddle.net/AndyE/ycmPt/show/ This works in Chrome 9, Firefox 4, Safari 5, Opera 11.50 and in IE … Read more

What’s the shebang/hashbang (#!) in Facebook and new Twitter URLs for?

This technique is now deprecated. This used to tell Google how to index the page. https://developers.google.com/webmasters/ajax-crawling/ This technique has mostly been supplanted by the ability to use the JavaScript History API that was introduced alongside HTML5. For a URL like www.example.com/ajax.html#!key=value, Google will check the URL www.example.com/ajax.html?_escaped_fragment_=key=value to fetch a non-AJAX version of the contents.

Should I make HTML Anchors with ‘name’ or ‘id’?

According to the HTML 5 specification, 5.9.8 Navigating to a fragment identifier: For HTML documents (and the text/html MIME type), the following processing model must be followed to determine what the indicated part of the document is. Parse the URL, and let fragid be the <fragment> component of the URL. If fragid is the empty … Read more