JavaScript window.location不在请求头中设置referer [英] JavaScript window.location does not set referer in the request header
问题描述
window.location
,为什么IE不会将Referer设置为Request Header?任何想法或修正? 这不会在请求标头中设置Referer :
function load1(){
window.location =https://+ serverURL +/path/folder/page.aspx;
}
< a href =javascript:load1()>链接1< / a>
设置:
< a href =https://hardcode.server.url/path/folder/page.aspx>连结1< / a>
您的文章标题显示您想更改当前页面以编程方式使用JavaScript,但仍然提供了HTTP引用者(根据我的理解,使用< a>
标签仅用于测试用例)。
$ b
您需要注意跨浏览器问题:
window.location.href
时设置了Referer):
myLink.click()
) window.location.href
/ li>
点击
函数不存在(这是为什么基于 myLink.click()
的伪解决方案无效)
点击
函数存在于Firefox 5下,但
没有改变窗口的位置,所以所有依赖于
存在的方法 myLink.click()
方法不起作用。调用 myLink.onclick()
或 myLink.onClick()
引发错误(onclick不是函数) ,所以解决方案基于在这些调用中不起作用。
为了管理这些跨浏览器问题,我使用以下方法:
function navigateToUrl(url){
var f = document.createElement(FORM);
f.action = url;
var indexQM = url.indexOf(?);
if(indexQM> = 0){
//该网址的参数=>将它们转换为隐藏的表单输入
var params = url.substring(indexQM + 1).split(&);
for(var i = 0; i< params.length; i ++){
var keyValuePair = params [i] .split(=);
var input = document.createElement(INPUT);
input.type =hidden;
input.name = keyValuePair [0];
input.value = keyValuePair [1];
f.appendChild(input);
}
}
document.body.appendChild(f);
f.submit();
}
navigateToUrl(http://foo.com/bar);
此解决方案适用于上面列出的所有浏览器风格和版本。它具有简单,多浏览器和易于理解的优点。
请注意,这尚未在HTTP S 下进行测试。
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>
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:
- The HTTP referrer header (HTTP-Referer) is set when changing
window.location.href
under the following browsers:- MSIE 9 (but probably any version above 9)
- Firefox (at least 3.0, 3.5, 4.0, 5.0, but most probably all versions)
- Chrome (at least 9, but most probably all versions)
- Safari (at least 5, but most probably all versions)
- Opera (at least 11, but most probably all versions)
- MSIE (at least 6, 7, 8): the referrer is not set when changing
window.location.href
(this is why some pseudo-solutions are based onmyLink.click()
) - Firefox (at least 3.0, 3.5, 4.0): the
click
function does not exist (this is why pseudo-solutions based onmyLink.click()
do not work) - Firefox 5 : the
click
function exists under Firefox 5 but does not change the window location, so all the methods relying on the existence of themyLink.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:
function navigateToUrl(url) {
var f = document.createElement("FORM");
f.action = url;
var indexQM = url.indexOf("?");
if (indexQM>=0) {
// the URL has parameters => convert them to hidden form inputs
var params = url.substring(indexQM+1).split("&");
for (var i=0; i<params.length; i++) {
var keyValuePair = params[i].split("=");
var input = document.createElement("INPUT");
input.type="hidden";
input.name = keyValuePair[0];
input.value = keyValuePair[1];
f.appendChild(input);
}
}
document.body.appendChild(f);
f.submit();
}
navigateToUrl("http://foo.com/bar");
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.
这篇关于JavaScript window.location不在请求头中设置referer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!