Home  >  Article  >  Web Front-end  >  2 solutions to the problem of missing referrer in JS jump in IE_javascript skills

2 solutions to the problem of missing referrer in JS jump in IE_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:41:421407browse

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:

Copy code The code is as follows:

function goToPage(url) {
If (isIE) {
// IE browser
        var frm = document.createElement("form");
frm.action = url;
frm.method = "GET";
           document.body.appendChild(frm);
frm.submit();
} else {
//Non-IE
location.href = url;
}
}

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:

Copy code The code is as follows:

//define for all browsers
function goto(url) {
Location.href = url;
}

//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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn