Today, I found a place that is easy to make mistakes when writing code. When we declare variables, we often have different API definitions because different browsers have different browsers, and then use the following writing method to determine which attribute is, for example:
The code copy is as follows: var fullscreenElement = document.mozFullScreenElement || document.webkitFullscreenElement || document.fullscreenElement;
Use || to check which attribute to use.
But be careful about the judgment when the value of javascript is regarded as a condition.
For example:
Copy the code as follows: var sLeft = window.screenLeft || window.screenX; //firefox use screenX
console.log(sLeft);
This code hopes that screenLeft will return window.screenLeft, and firefox will return window.screenX.
However, if screenLeft just equals 0, it will enter the following conditions, and then gg.
Therefore, it is recommended to use hasOwnProperty or typeof to judge the value more accurately.
The code copy is as follows: var sLeft = window.screenLeft;
if( !window.hasOwnProperty('screenLeft')) sLeft = window.screenX;