Firefox href bug


AuthorAnton Savchenko (Software Engineer)

Level: Beginner

Description: knows javascript, getting started with browser extension development

window.location is an object which contains information about the document URL. This object also exposes methods for URL modification. There is a known bug in Firefox when retrieving the location.hash property. This property returns the URL following the hash symbol(#), however unlike other browsers or any other location property the returned portion of the URL is automatically decoded.

results in:

  • hash=#Fire fox
  • search=?q=Fire%20fox

In order to simulate the same behavior as the other browsers the hash property should be passed to encodeURIComponent method.For more information on the location property check out the full documentation here.

Just to reiterate the point this issue only exists in Firefox and its a known bug. It’s likely that the behavior of the location.hash property will change in the future to match the other location properties.

Code example, use this for Firefox only!

var locationObject = Window.location;
var correctHash = encodeURIComponent(location.hash);

%d bloggers like this: