Setting JavaScript window.location
Asked Answered
B

4

51

I'm currently setting the window.location.pathname property to redirect the user to a relative URL. The new URL has parameters, so the line of JavaScript looks like this:

window.location.pathname = window.location.pathname.substring( 0, window.location.pathname.lastIndexOf( '/' ) + 1 ) + 'myPage.xhtml?u=' + selected_user.Username;

This is successful in Firefox, however Chrome encodes the question mark with '%3F' and the request subsequently fails.

I'm not sure if I'm using window.location properly. Do I need to use properties of window.location such as pathname or href? I've found that as soon as I set one property the location is reloaded, so for example, the search and pathname properties can't be set separately. Can window.location be set directly? I only need to set a relative URL with a parameter.

Bola answered 4/9, 2010 at 16:14 Comment(0)
E
69

pathname and many other properties of location and links reflect only part of the URL:

http:  //www.example.com/path/to/example.html?param1=2&param3=4#fragment
^protocol^hostname      ^pathname            ^search           ^hash

As you can see, the ?... part of the URL is not part of the pathname; it makes no sense to write a value containing ? to location.pathname, as that part of a URL cannot contain a question mark. Chrome is correcting your mistake by encoding the character to a sequence that means a literal question mark, which doesn't terminate pathname.

These properties are great for breaking a URL into their constituent parts for you to process, but you probably don't want to write to them in this case. Instead, write to location.href. This represents the whole URL, but it's perfectly fine to write a relative URL to it; this will be worked out relative to the current value, so there is in fact no need to read and split the pathname at all:

location.href= 'myPage.xhtml?u='+encodeURIComponent(selected_user.Username);

Note the URL-encoding. If a username can contain characters other than alphanumerics you will probably need this to stop those characters breaking the parameter. Always URL-encode arbitrary strings before putting them into part of a URL.

Evolutionist answered 4/9, 2010 at 19:22 Comment(3)
Thanks for the detailed explanation. I've changed my code to use the href property and also to call encodeURIComponent().Bola
Please note that for most cases, you want to use location.host instead of location.hostname. In short, this is because your code at some point may run in a server on a port other than 80.Irksome
Note that in (rare) case when the source is an iframe, then it needs to do window.parent.location.....Brindle
R
17

Try setting the location.href property instead of window.location.pathname.

Robespierre answered 4/9, 2010 at 16:18 Comment(2)
just window.location = ... is enoughFuthark
Assigning a value to window.location causes an error in TypeScript. I guess assigning to window.location.href is the better option.Leucoderma
M
9

Using window.location.href is considered the safest way to set a URL. I think this should fix the encoding problem.

window.location.href = window.location.pathname.substring( 0, window.location.pathname.lastIndexOf( '/' ) + 1 ) + 'myPage.xhtml?u=' + selected_user.Username;

If that doesn't help, please show an example URL.

Magistrate answered 4/9, 2010 at 16:19 Comment(0)
G
0

In your case you can set the location.search property without having to know/reset/decode the entire URL.

location.search = 'u=' + selected_user

Or if you have a lot of parameters with lots of different kinds of characters

location.search = 
    new URLSearchParams({
       u: selected_user,
       otherThing: "hi this %#%##^ is a "+
          "Very •}€○♡●¡● complex string"

    });

Garnet answered 14/11, 2023 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.