2 solutions to referrer problem of JS jump loss in IE _javascript tips

Source: Internet
Author: User
Tags goto

has collated a variety of page jump method referrer lost situation, which mentioned, in IE, using similar location.href = "a.html" in such a way to jump the page, in the target page Document.referrer value will be empty. This should be a bug for IE.

In most cases, this problem will not cause us any trouble, but sometimes we have to use JavaScript to jump, but also in the next page to collect Document.refer, then we have to think of other ways.

Form Get method

The first thought is to use form form, with JS to launch a GET request. The code resembles the following:

Copy Code code 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, and the document.referrer on the target page will normally point to the previous page.

A element analog click Method

A search on the internet found that Masaki's blog recorded another way to deal with the problem:

Copy Code code 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 simple, first create a a element, specify its href attribute as the target link, and then use JS to trigger its click events. After testing, in the target page can also be normal access to document.referrer.

This method code is a little bit shorter and should be better than the one above using the form form.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.