Your location:Tech News>News>Script>15 very useful JavaScript code snippet

Latest News

15 very useful JavaScript code snippet

Author:fsadmin

Views:

  Examples herein, share a very practical js fragment, for your reference, as follows

  1.How to distinguish between IE and non-IE browsers:

  if (!+ [1,]) {// IE 11 does not support

  alert ( "This is the IE browser");

  } Else {

  alert ( "This is not the IE browser");

  }

  2.The date directly converted to values:

  + New Date ();

  3.Non-IE browsers like array of objects "arguments" into the array:

  Array.prototype.slice.call (arguments);

  4.The simplest option operator ||:

  var a = 0 || 3;

  console.log (a); // Results

  If the latter value = first calculation result is a Boolean "true", the value of a first one taken, otherwise, it is the second.

  5.Single calculation chain (e.g., a ++ -1):

  var a = 10;

  console.log (a ++ -1);

  To perform "a-1", and then the implementation of "a = a + 1".

  6.Interestingly void operator:

  I am a dead link

  void is an operator for calculating an expression, but does not return a value.

  7.Jump to a new page, and ensure that the browser does not return back:

  location.replace ( "https: // www.jb51.net ");

  The location replace () method can replace the current document with a new document, and the method also covers the recording object History.

  8.Back after a few seconds:

  

  9.In the child window open to refresh the parent window:

  window.opener.location.reload ();

  10.Verify negative regular expression:

  / ^ - \ d + $ /.test (str);

  11.Print page using JavaScript:

  window.print ()

  12.Show / Hide a DOM element:

  el.style.display = "";

  el.style.display = "none"; // el DOM element to be operated

  Display DOM element / Hide mainly achieved by setting the style display attribute elements.

  13.Implement text alert () line in the exchange:

  alert ( "p \ np")

  "\ N" represents the newline.

  14.The Object achieve ECMAScript5.create () function:

  function clone (proto) {

  function _clone () {}

  _clone.prototype = proto;

  _clone.prototype.constructor = _clone;

  return new_clone (); // equivalent to Object.create (Person);

  var me = clone (Person);

  Inherited prototype chain form, the constructor redirected to the newly created object.

  15.Closures appreciated JavaScript:

  For example, the following code will output five times, the result is 5, then how output 0,1,2,3,4?

  for (var i = 0; i < 5; i++) {

  setTimeout (function () {

  console.log (i);

  }, 1000);

  }

  Closure implemented using the principles, as follows:

  for (var i = 0; i < 5; i++) {

  (Function (e) {

  setTimeout (function () {

  console.log (e);

  }, 1000);

  }) (I);

  }

  That''s all for this article, I want to be helpful to learn, I hope you will support script Home.

  You may also be interested in the article: work in common js, jquery custom extension functions snippet summary beautiful code fragment JS cross-domain JavaScript code snippets and practical JQuery code snippets you''ll collection Nodejs snippet of JavaScript (a) summary JS AJAX-related code snippets and browser model allows a very simple picture automatically switches js code for a powerful javascript code decryption methods together to write the code segment JS drag drag those wonderful JavaScript code snippet

Recommend article

Relate article