设置 JavaScript window.location
我当前正在设置 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;
这在 Firefox 中是成功的,但是 Chrome 将问号编码为“%3F”,请求随后失败。
我不确定我是否正确使用了 window.location 。我是否需要使用 window.location 的属性,例如路径名或 href?我发现一旦设置一个属性,位置就会重新加载,因此,例如,不能单独设置搜索和路径名属性。 window.location可以直接设置吗?我只需要设置一个带有参数的相对URL。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
pathname
以及location
和链接的许多其他属性仅反映 URL 的部分:如您所见,
?... URL 的
部分不是路径名
的一部分;将包含?
的值写入location.pathname
是没有意义的,因为 URL 的该部分不能包含问号。 Chrome 正在通过将字符编码为表示字面问号的序列来纠正您的错误,该序列不会终止路径名
。这些属性非常适合将 URL 分解为其组成部分以供您处理,但在这种情况下您可能不想写入它们。相反,请写入
location.href
。这代表了整个 URL,但写一个相对 URL 也完全没问题;这将相对于当前值计算出来,因此实际上根本不需要读取和分割路径名
:请注意 URL 编码。如果用户名可以包含字母数字以外的字符,您可能需要它来阻止这些字符破坏参数。在将任意字符串放入 URL 的一部分之前,始终对它们进行 URL 编码。
pathname
and many other properties oflocation
and links reflect only part of the URL:As you can see, the
?...
part of the URL is not part of thepathname
; it makes no sense to write a value containing?
tolocation.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 terminatepathname
.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 thepathname
at all: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.
尝试设置
location.href
属性而不是window.location.pathname
。Try setting the
location.href
property instead ofwindow.location.pathname
.使用
window.location.href
被认为是设置 URL 的最安全方法。我认为这应该解决编码问题。如果这没有帮助,请显示示例 URL。
Using
window.location.href
is considered the safest way to set a URL. I think this should fix the encoding problem.If that doesn't help, please show an example URL.
在您的情况下,您可以设置
location.search
属性,而无需知道/重置/解码整个 URL。或者如果你有很多参数和很多不同类型的字符
In your case you can set the
location.search
property without having to know/reset/decode the entire URL.Or if you have a lot of parameters with lots of different kinds of characters