How to solve IE window.location.href not sending Referer

JavaScript judges the most perfect solution for each version of IE

Before the 1.9 version, jQuery provided a browser object detection property $.browser, which was very used. But after the 1.9 release, the beloved property was ruthlessly abandoned by jQuery. People started looking for an alternative to $.browser. So various detection methods using IE bugs were found:

// shortest from a Russian
var ie = !-[1,]

// Option from Dean Edwards:
var ie = /*@cc_on!@*/false

// Use the commented line:
var ie//@cc_on=1

// Variation (shorter variable):
var ie = '\v'=='v'

// Option to Gareth Hayes (former record-holder):
var ie = !+"\v1"

There are also some ways to read navigator.userAgent. In Apple's view, these notations are not friendly enough and not easy to remember. After searching and selecting, I finally found an easy-to-understand and friendly and convenient way to write!

solution

IE knows that it has many problems, so it provides a set of official HTML hack methods:

<!--[if IE]>
// 全部IE版本可见
<![endif]-->
<!--[if IE 6]>
// IE6可见
<![endif]-->

and so on.
In other browsers, this way of writing is just a bunch of comments and is directly ignored, but it is not in IE. IE will analyze the version number mentioned in it, and determine whether to parse the DOM element and text content according to the version number. Wait a moment! DOM elements? Wouldn't it be possible to use js to get the DOM elements inside? Anyway, whoever sees it is IE! Therefore, the foreign gods have the following writing:

var isIE = function(){
    var b = document.createElement('b')
    b.innerHTML = '<!--[if IE]><i></i><![endif]-->'
    return b.getElementsByTagName('i').length === 1
}

This is so ingenious too! First, a b element is generated, and its innerHTML is set as a comment that only IE knows, and there is only one empty tag in the comment, and then read whether the number of elements i appearing in it is equal to 1, or is it equal to 1 , is not equal to 1. . . .
In the Big Apple's view, this way of writing is better than any other. As for why a b element is generated and an i element is written in it instead of div or strong, it is more considering that the former has a smaller amount of bytes.
The method of detecting each IE version is logical:

var isIE6 = function(){
    var b = document.createElement('b')
    b.innerHTML = '<!--[if IE 6]><i></i><![endif]-->'
    return b.getElementsByTagName('i').length === 1
}
// var isIE7 
// ...

Go one step further

In Apple's view, the version number can be further extracted as a parameter, and a general function to detect the IE version can be generated:

var isIE = function(ver){
    var b = document.createElement('b')
    b.innerHTML = '<!--[if IE ' + ver + ']><i></i><![endif]-->'
    return b.getElementsByTagName('i').length === 1
}
if ( isIE ( 6 )) {
     // IE 6
}
// ... 
if ( isIE ( 9 )) {
     // IE 9 
}

This way, there is no pressure to test which version. However, if you only want to detect whether it is IE and don't care about the browser version, you only need to pass no parameters when calling the function.

var ie   =  isIE ()

Finally, paste the screenshots of the test code in major browsers in turn.

alert ( ' ie6: '  +  isIE ( 6 ) +  ' \ n '  +  ' ie7: '  +  isIE ( 7 ) +  ' \ n '  +  ' ie8: '  +  isIE ( 8 ) +  ' \ n '  +  ' ie9: '  +  isIE (

 

 

====================================================================================

JavaScript client-side scripting language

Javascript is a prototypically inherited object-based dynamic type case-sensitive client-side scripting language developed from Netscape's LiveScript. The main purpose is to solve the speed problem of server-side languages, such as Perl, for clients Provides smoother browsing.

 


 


HTTP Header referer This is mainly to tell people which page I came from. It can be used to count the source of users who visit this website, and it can also be used to prevent chain protection. The best way to get this thing is js. If it is obtained on the server side (PHP method such as: $_SERVER['HTTP_REFERER']), it is unreliable and can be faked. It is best to use js to obtain it and it is difficult to fake it.

Method: Use the document.referer method of js to accurately determine the true origin of the web page. At present, Baidu statistics, google ads statistics, and CNZZ statistics all use this method. The anti-theft chain is also very simple. If the url is judged in js, the picture will not be displayed if it is not this site.
As we all know, we web developers hate IE browser, because IE does not support standards, and the default behavior outside the standard is often inconsistent with other browsers:
use javascript to jump in IE, such as using window.location.href = ""; If ie uses document.referrer, the HTTP referrer requested by the browser cannot be obtained, because IE is cleared
and other mainstream browsers Firefox and Chrome will retain the referrer. There is no way, which means that IE has to enjoy the "ministerial" special treatment again:
The following code can solve this problem of ie:
//Detect if it is an ie browser, then manually add a referer

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || /MSIE(\d+\.\d+);/.test(navigator.userAgent)){
    var referLink = document.createElement ('a');
    referLink.href = url;
    document.body.appendChild(referLink); referLink.click
    (); }
else {
    location.href = url; a link, and then click the link automatically, so the referrer can be retained.

 

 

=================================Project Application ================= ===========

// Determine if IE
function isIE() {
 return ("ActiveXObject" in window);
}

/**
 * Under IE, when using window.loaction.href=url, window.open(url), the referer will be lost, this method should be used instead of
 * window.location.reload() is no problem
 *
 * @param {} url new link
 * @param {} newTab whether to open a new window
 */
function gotoUrl(url, newTab) {
 if (isIE()) {
  if (newTab) {
   var referLink = document.createElement('a');
   referLink.href = url;
   referLink.target = "_blank";
   document.body.appendChild(referLink);
   referLink.click();
   document.body.removeChild(referLink);
  } else {
   var referLink = document.createElement('a ');
   referLink.href = url;
   document.body.appendChild(referLink);
   referLink.click();
  }
 } else {
  if (newTab) {
   window.open(url);
  } else {
   window.location.href = url;
  }
 }
}

 

 

Reference address:

http://www.phperz.com/article/14/0702/2914.html

 

https://github.com/nioteam/jquery-plugins/issues/12

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326181613&siteId=291194637