Home >Web Front-end >JS Tutorial >2 solutions to the problem of missing referrer in JS jump in IE_javascript skills
I once compiled a referrer loss situation in various page jump methods, which mentioned that in IE, use a method like location.href = "a.html" to jump to the page , the value of document.referrer in the target page will be empty. This should be a bug in IE.
In most cases, this problem will not cause us trouble, but sometimes we have to use JavaScript to jump and at the same time collect document.refer on the next page, then we have to think of other methods.
Form GET method
The first thing that comes to mind is to use the Form form and initiate a GET request with JS. The code is similar to the following:
This method works as expected, document.referrer in the target page can point to the previous page normally.
A element simulation click method
After searching online, I found another solution to this problem recorded on Situ Zhengmei’s blog:
//re-define for IE
if (isIE) {
Function goto(url) {
var referLink = document.createElement('a');
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
}
The principle is very simple. First create an A element, specify its href attribute as the target link, and then use JS to trigger its click event. After testing, document.referrer can be obtained normally on the target page.
The code of this method is shorter and should be better than the above solution using form.