0

I want to change pathname via function window.location.pathname. I have got this source code.

var hash = window.location.hash;
window.location.pathname = hash;

in Mozilla it works right, but in Chrome doesn't. Chrome write me this adress.

/%23!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783?stranka=kontakty#!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783

Value of hash is #!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783

Have someone any idea?

Thanks.

apsillers
  • 112,806
  • 17
  • 235
  • 239
David Holada
  • 109
  • 1
  • 2
  • 14
  • 1
    Possible duplicate of http://stackoverflow.com/questions/3643041/setting-javascript-window-location – radimpe Jun 19 '12 at 12:17

1 Answers1

1

You'll have to understand that location.hash includes the # itself as well. The rest of the location.hash is, per spec, URLencoded, but the # isn't.

I said "per spec", as Firefox has a bug related to the location.hash property.

If you want to change location.pathname to the hash with the value included, you'll have to encode the # before doing so.

Example:

var hash=location.hash.substring(1)
location.pathname='%23'+hash

If you don't want the hash to be included, just use

var hash=location.hash.substring(1)
location.pathname=hash
user2428118
  • 7,935
  • 4
  • 45
  • 72