Webkit 将“#”转为进入 %23 导致 404 错误?
我只是构建一个简单的 ajax 网站,但在 safari 和 chrome 中遇到问题。我只是创建一个简单的重定向,如果用户转到某个页面,即
"/blog"
他们将被重定向到
"/#/blog"
代码:
url = window.location.pathname
if(url != "/") {
window.location.pathname = "/#" + url
}
这在 FireFox 中运行良好,但不幸的是 webkit 浏览器将“#”转换为“%23”并给出 404,例如:
"/%23/blog"
我怎样才能防止这种情况发生?
谢谢, 亚历克斯
I'm just building a simple ajax site but running into a problem in safari and chrome. I'm just creating a simple redirect if the user goes to a page i.e.
"/blog"
they would be redirected to
"/#/blog"
code :
url = window.location.pathname
if(url != "/") {
window.location.pathname = "/#" + url
}
This is working great in FireFox but unfortunatly webkit browsers are turning the "#" into a "%23" and giving a 404, for example:
"/%23/blog"
How can I prevent this?
Thanks,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pathname
指的是主机之后、查询字符串和哈希之前的所有内容。请考虑这一点:我不确定哪些浏览器正确实现了 JavaScript 规范,但 WebKit 的行为对我来说似乎是正确的。
pathname
refers to everything after the host, and before the query string and hash. Consider this instead:I'm not exactly sure which browser(s) are implementing the JavaScript spec correctly, but WebKit's behavior seems correct to me.
您正在设置路径名,根据定义,该路径名不包含哈希值。 Webkit 正在尝试为您解决这个问题(Firefox 只是更好地猜测您想要什么)。试试这个:
You are setting pathname, which, by definition, does not include the hash. Webkit is trying to fix that for you (Firefox just made a better guess of what you wanted). Try this: