设置 JavaScript window.location

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3643041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:33:32  来源:igfitidea点击:

Setting JavaScript window.location

javascriptwindow.location

提问by Mark

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 属性以将用户重定向到相对 URL。新的 URL 有参数,所以这行 JavaScript 看起来像这样:

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.

这在 Firefox 中是成功的,但是 Chrome 使用“%3F”对问号进行编码,随后请求失败。

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.

我不确定我是否正确使用 window.location。我是否需要使用 window.location 的属性,例如路径名或 href?我发现一旦我设置了一个属性,位置就会重新加载,例如,不能单独设置搜索和路径名属性。window.location 可以直接设置吗?我只需要设置一个带有参数的相对 URL。

回答by bobince

pathnameand many other properties of locationand links reflect only partof the URL:

pathnamelocation链接的许多其他属性仅反映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.

如您所见,?...URL的一部分不是pathname; 写入包含?to的值是没有意义的location.pathname,因为 URL 的那部分不能包含问号。Chrome 正在通过将字符编码为表示字面问号的序列来纠正您的错误,该序列不会终止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 pathnameat all:

这些属性非常适合将 URL 分解为它们的组成部分供您处理,但在这种情况下您可能不想写入它们。相反,写入location.href. 这表示整个 URL,但写一个相对 URL 到它是完全没问题的;这将相对于当前值计算出来,因此实际上根本不需要读取和拆分pathname

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.

注意 URL 编码。如果用户名可以包含字母数字以外的字符,您可能需要它来阻止这些字符破坏参数。在将任意字符串放入 URL 的一部分之前,始终对其进行 URL 编码。

回答by Chris Laplante

Try setting the location.hrefproperty instead of window.location.pathname.

尝试设置location.href属性而不是window.location.pathname.

回答by Pekka

Using window.location.hrefis considered the safest way to seta URL. I think this shouldfix the encoding problem.

使用window.location.href被认为是设置URL的最安全方式。我认为这应该可以解决编码问题。

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.

如果这没有帮助,请显示示例 URL。