Gregory Kalabin
Gregory Kalabin

Reputation: 1788

Special characters in url in Safari

We are using some special characters in our web application like this: example.com/foo#вап.

We parse hash using decodeURI(window.location.hash) (and sometimes hash contains not encoded special characters) and set new value like window.location.hash = "вап".

Everything works fine in Chrome, Firefox, Opera and even IE, but in Safari we get 20? instead вап.

If set hash in Safari like window.location.hash = encodeURI("вап"); it works, but of course it doesn't work in Chrome, FF and others.

Upvotes: 4

Views: 2786

Answers (2)

rab
rab

Reputation: 4144

I have faced same issue, found another workaround

window.location.hash = path;
// For safari url change fix
if (window.location.hash !== path) {
    window.location.hash = encodeURI(path);
}

Upvotes: 0

Gregory Kalabin
Gregory Kalabin

Reputation: 1788

Finally I found the solution. If set hash through window.location.href everything works fine.

Here is the code:

var newHash = ...
var sharpIdx = window.location.href.indexOf("#");
if (sharpIdx === -1) {
  window.location.href = window.location.href + "#" + newHash;
} else {
  window.location.href = window.location.href.substr(0, sharpIdx) + "#" + newHash;
}

Upvotes: 5

Related Questions