如何防止window.location.pathname在URL中转义字符

How to prevent window.location.pathname from escaping characters in URL

本文关键字:URL 转义字符 pathname 何防止 window location      更新时间:2023-09-26

我正在开发一个谷歌应用程序引擎应用程序。当点击一个按钮时,我正试图将用户重定向到登录页面。为此,我将window.location.pathname设置为后面的字符串/_ah/login?continue=http%3A//localhost%3A8080/。但是,chrome对字符串进行转义,使完整的URL变为http://localhost:8080/_ah/login%3Fcontinue=http%3A//localhost%3A8080/,这会产生404错误。我该如何防止这种情况发生?工作的URL是http://localhost:8080/_ah/login?continue=http%3A//localhost%3A8080/

改为设置window.location.href

我认为你最好只使用window.location.href。在Chrome和Firefox中,window.location.href="/?foofoo"重定向到<domain:port>/?foofoo

您可以在javascript中使用以下代码来解码URI组件:

decodeURIComponent(window.location.pathname)