设置 JavaScript window.location

发布于 2024-09-17 01:01:48 字数 496 浏览 15 评论 0原文

我当前正在设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一瞬间的火花 2024-09-24 01:01:48

pathname 以及 location 和链接的许多其他属性仅反映 URL 的部分

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

如您所见,?... URL 的 部分不是路径名的一部分;将包含 ? 的值写入 location.pathname 是没有意义的,因为 URL 的该部分不能包含问号。 Chrome 正在通过将字符编码为表示字面问号的序列来纠正您的错误,该序列不会终止路径名

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

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

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

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¶m3=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.

紫轩蝶泪 2024-09-24 01:01:48

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

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

不奢求什么 2024-09-24 01:01:48

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

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

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

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.

痞味浪人 2024-09-24 01:01:48

在您的情况下,您可以设置 location.search 属性,而无需知道/重置/解码整个 URL。

location.search = 'u=' + selected_user

或者如果你有很多参数和很多不同类型的字符

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

    });

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"

    });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文