This example for you to share a very practical JS clips for your reference, the specific contents are as follows
1. How to distinguish IE and non ie browsers:
if (!+[1,]) {//ie 11 does not support
alert ("This is IE browser");
}else{
alert ("This is not IE browser");
}
2. Convert a date directly to a numeric value:
+new Date ();
3. Convert the class Array object "Arguments" to an array in non-IE browsers:
Array.prototype.slice.call (arguments);
4. The simplest choice operator | | :
var a = 0 | | 3;
Console.log (a); Result 3
If the first value that follows evaluates to the Boolean "true", the value of a is 1th, otherwise the 2nd is taken.
5. Single-chain operation (e.g. a++-1):
var a=10;
Console.log (a++-1);
Executes "A-1" first, then executes "a=a+1".
6. Interesting void operators:
<a href= "javascript:void (0)" > I am a dead link </a>
Void is an operator that evaluates an expression but does not return a value.
7. Jump to the new page and make sure that the browser will not be rolled back:
location.replace ("http://www.jb51.net");
The location replace () method can replace the current document with a new document, and the method also overwrites the records in the History object.
8. After a few seconds to return to the previous page:
<meta http-equiv= "Refresh" content= "3;url=javascript:window.history.go (-1);" >
9. Refresh the parent window in the Open child window:
Window.opener.location.reload ();
10. A regular expression that verifies whether it is a negative number:
/^-\d+$/.test (str);
11. Print pages in javascript:
Window.print ()
12. Show/Hide a DOM element:
El.style.display = "";
El.style.display = "None"; El is the DOM element to be manipulated
The display/hide of the DOM element is implemented primarily by setting the style display property of the element.
13. Implement text wrapping in alert ():
Alert ("P\NP")
\ n stands for line breaks.
14. Implement the Object.create () function in ECMAScript5:
function Clone (proto) {
function _clone () {}
_clone.prototype = Proto;
_clone.prototype.constructor = _clone;
return New_clone (); Equivalent to object.create (person);
var me = clone (person);
Inherits with a prototype chain, and the constructor points back to the newly created object.
15. Understanding the closures in javascript:
For example, the following code will output 5 times and the result is 5, so how do you output 0, 1, 2, 3, 4?
for (var i = 0; i < 5; i++) {
settimeout (function () {
console.log (i);
}, 1000);
}
Using the principle of closure, the code is as follows:
for (var i = 0; i < 5; i++) {
(function (e) {
settimeout (function () {
console.log (e);
}, 1000);
}) (i);
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.