JavaScript window.location 未在请求标头中设置引用地址
我知道在请求标头中依赖 Referer 是不正确的。但我的问题是,如果我使用 window.location
,为什么 IE 不将 Referer 设置为请求标头?有什么想法或修复吗?
这不会在请求标头中设置 Referer:
function load1() {
window.location = "https://" + serverURL + "/path/folder/page.aspx";
}
<a href="javascript:load1()">Link 1</a>
虽然设置了:
<a href="https://hardcode.server.url/path/folder/page.aspx">Link 1</a>
I understand relying on Referer in the request header is not right. But my question is, why IE does not set Referer to the Request Header if I use window.location
? Any thoughts or fixes?
This does not set Referer in the Request header:
function load1() {
window.location = "https://" + serverURL + "/path/folder/page.aspx";
}
<a href="javascript:load1()">Link 1</a>
While this sets:
<a href="https://hardcode.server.url/path/folder/page.aspx">Link 1</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的帖子标题显示您希望使用 JavaScript 以编程方式更改当前页面,但仍提供 HTTP 引用(据我所知,使用
标记仅用于测试用例)。
您需要注意跨浏览器问题:
window.location.href
时会设置 HTTP 引用标头 (HTTP-Referer):window.location.href
时未设置引荐来源网址(这就是为什么某些伪解决方案基于myLink.click()
)click
功能不存在(这就是为什么基于myLink.click()的伪解决方案
code> 不起作用)click
功能在 Firefox 5 下存在,但不存在更改窗口位置,因此所有方法都依赖于
myLink.click()
方法的存在将不起作用。调用myLink.onclick()
或myLink.onClick()
会引发错误(“onclick 不是函数”),因此 基于这些调用的解决方案将不起作用。为了管理这些跨浏览器问题,我使用以下方法:
此解决方案适用于上面列出的所有浏览器风格和版本。它的优点是简单、多浏览器、易于理解。
请注意,这尚未在 HTTPS 下进行测试。
Your post title shows that you want to change the current page programmatically using JavaScript but still having the HTTP referrer provided (from what I understood, using a
<a>
tag is just for a test case).You need to be aware of cross-browser issues:
window.location.href
under the following browsers:window.location.href
(this is why some pseudo-solutions are based onmyLink.click()
)click
function does not exist (this is why pseudo-solutions based onmyLink.click()
do not work)click
function exists under Firefox 5 but does notchange the window location, so all the methods relying on the
existence of the
myLink.click()
method will not work. CallingmyLink.onclick()
ormyLink.onClick()
raise an error ("onclick is not a function"), so solutions based on these calls will not work.In order to manage these cross-browser issues, I'm using the following method:
This solution works on all the browser flavors and version listed above. It has the advantage to be simple, multi-browser and easy to understand.
Note that this has not been tested under HTTPS.
设置
window.location
与跟踪该页面上的链接不同。当用户在浏览器地址栏中输入 URL 时,它会启动一个新的页面请求。我确实设法找到了解决方法:
它在页面上创建一个链接并模拟单击。结果是
window.location
发生更改,并填充引荐来源网址。http://ianso.blogspot.com/2006 /01/referer-header-not-set-on-http.html
Setting
window.location
is not the same as following a link on that page. It starts a new request for the page as thought the user typed the URL into the browser's address bar.I did manage to locate a workaround:
It creates a link on the page and simulates a click. The result is a change in
window.location
and the referrer is populated.http://ianso.blogspot.com/2006/01/referer-header-not-set-on-http.html
我没有足够的观点来评论埃文的答案来建议更正,所以我所能做的就是在这里发布更正。简而言之,
document.createElement(a)
缺少引号,应改为document.createElement("a")
。这应该也能解决 Kevin 对 FF5 的担忧。我写的整个函数:
这适用于我们的 HTTPS 环境,支持 IE7、IE8、FF3、FF7 和 Chrome。所以我想它也适用于 FF5。如果没有此解决方法,当尝试设置 window.location 时,我们会在 IE7 和 IE8 中收到 403 错误。对于沙乐提出的IE为什么要这样做的问题,我只能猜测是他们认为IE太不安全了。我在 IE 中遇到了类似的 window.open 问题,我也必须解决这个问题。
I don't have enough points to comment on Evan's answer to suggest a correction so all I can do is post the correction here. In short,
document.createElement(a)
is missing quotes and should bedocument.createElement("a")
instead. This should fix Kevin's concern about FF5 as well.The whole function as I wrote it:
This works in our HTTPS environment with IE7, IE8, FF3, FF7, and Chrome. So I imagine it works in FF5 as well. Without this workaround we get 403 errors in IE7 and IE8 when trying to set window.location. Regarding Sha Le's question as to why IE does this, I can only guess is that they believe it to be too insecure. I had a similar problem with window.open in IE that I had to work around as well.
是否可以通过 JavaScript 触发链接(或任何元素)的点击事件? 使用 createEvent/dispatchEvent 或 createEventObject/fireEvent 解决方案。
Is it possible to trigger a link's (or any element's) click event through JavaScript? uses a createEvent/dispatchEvent or createEventObject/fireEvent solution.
好的。我有同样的问题,但我想在我的 Scrapper 应用程序中重定向 URL。我在本地主机上运行我的 scrapper 应用程序,也在服务器上运行,并且服务器上我想重定向到的一些 URL 是 IP,所以我添加了 URL 验证。希望这对其他人有帮助。
示例:
OK. I have the same problem but with URLs that I wanna redirect in my Scrapper app .. I run my scrapper app on localhost and also on the server and some URLs on the server that i wanna redirect to are IPs so I added URL validation. hope this helps others.
Examples:
是的,你的也有效,但最终做了:
Yeap, yours works as well, but ended up doing: