A weird way to judge Internet explorer in JavaScript

  • 2020-03-30 02:38:02
  • OfStack

Remember on the Internet Down a lot of source code with the most one way is:
var ie = document.all();

This is because Internet explorer has an all method under document, while the so-called standard browsers do not, so this is a very common method for a long time.
After that, people began to pursue brevity, which was a popular method after that:
var ie = ! + "v1";

Taking advantage of the fact that IE does not support vertical tabs, a mere seven characters is shocking, but the record was broken earlier this year by a Russian who needed only six characters.
We won't discuss what this character can do for a moment, but think of it as a kind of study, or whatever it is that you call him idle.
var ie = ! - [1,];

In fact, the principle is very simple, [1,] in the standard browser will return the string "1", which is equivalent to calling [1,]. ToString, IE will return "1,". However, both IE and the standard will pass the detection, so use the minus sign to cast to a number, the standard can be successfully converted to 1,1 will be automatically converted to true in if, and IE to NaN, and then automatically converted to false! View the DEMO.
Simple, right? I'm sure there will be a lot of people thinking the same thing. Why didn't I think of that? Yeah, why didn't I think of that? (laughs ~)

Related articles: