Summarizing _javascript techniques of common native JS custom functions

Source: Internet
Author: User
Tags array length html page

JS Get Date function

Gets the current time date
function curenttime ()
{var now
  = new Date ();
  var year = Now.getfullyear ();    Year
  var month = Now.getmonth () + 1;   Month
  var day = Now.getdate ();      Day
  var hh = now.gethours ();      When
  var mm = Now.getminutes ();     cent
  var clock = year + "-";

  if (month <)
    clock + = "0";

  Clock + + month + "-";

  if (Day <)
    Clock + = "0";

  Clock = day + "";

  if (HH <)
    clock + = "0";

  Clock + = hh + ":";
  if (mm <) clock + = ' 0 ';
  Clock = mm;
  return (clock);
}

JS Gets the time difference function

Gets how many days of
time difference function getlasttime ()
  {
    var starttime=new Date ("1996-5-11 00:00");//start times
    var endtime= New Date ();  End Time
    var lasttime=endtime.gettime ()-starttime.gettime ()/////////() milliseconds///times

    difference number of days
    var days=math.floor ( lasttime/(24*3600*1000))

    //Calculate the number
    of hours var leave1=lasttime% (24*3600*1000)  //number of days after the calculation of the remaining milliseconds
    var hours= Math.floor (leave1/(3600*1000))
    //To calculate the number of minutes between
    var leave2=leave1% (3600*1000)    //The number of milliseconds remaining after calculation of
    var Minutes=math.floor (leave2/(60*1000))

    //Calculate the number
    of seconds between Var leave3=leave2% (60*1000)   //Count the remaining milliseconds
    after the number of minutes var seconds=math.round (leave3/1000) return

    "difference" +days+ "Days" +hours+ "hours" +minutes+ "minutes" +seconds+ "seconds";
  }

JS only automatically refresh page once

Automatically refresh the page once after stop refreshing
window.onload = function () {
  if (location.search.indexOf ("?") ==-1) {
   location.href = "? Myurl";
  }
  else{
   if (location.search.indexOf ("Myurl") ==-1) Location.href + = "&myurl";
  }
}

Ajax instances

$.ajax ({
    type: POST),
    URL: "join.php",
    data:datastring,
    success:function () {
      $ ('. Success '). FadeIn. Show ();
      $ ('. Error '). fadeout. Hide ();
    }
  });

Get window size in real time

$ (window). Resize (function () { 
  var height = $ (window). Height ();
  var width = $ (window). width ();
})

JS loop execution function and timed execution function

Loop execution, execute once every 3 seconds Showalert () 
  window.setinterval (Showalert, 3000);
  function Showalert ()
  {
    alert ("Loop Execution");
  }
  Scheduled execution, 5 seconds to execute show ()
  window.settimeout (show,5000);
   function Show ()
   {

    alert ("Timed execution");
   

JS gets get Parameter function

function getquerystring (name)
{
   var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
   var r = window.location.search.substr (1). Match (reg);
   if (r!=null) return unescape (r[2]); return null;
}
Alert (getquerystring ("parameter name 1"));

JS Page Print Array function

 /**
  * Print Array
  * @param {[type]} arr the  array to be printed
  * @param {[type]} space controls the indentation of the print
  @param {[Type]} SPACE2 controls the printed Indent 2
  /function Print_arr (arr, space, Space2)
  {spaces

  = Space | | ' ';

  Space2 = Space2 | | '     ';

  var str = "array<br>" +space+ (<br>);

  for (var i=0; i<arr.length; i++)

  {

   if (Object.prototype.toString.call (arr[i)) = = ' [Object Array] ')

   { Determine if it is an array, and if so, perform a recursive concatenation of

    str = space2 + ' [' +i+ '] => ' + Print_arr (arr[i], space+ '   , space2+ '   );

   Else

   {

    str + + Space2 + ' [' +i+ '] => ' + arr[i] + ' <br>

   }

  } ' STR + + space+ ") <br>";

  document.write (str);

 

JS print JSON data array form output in HTML

/** output space function */function blank (num) {var res = ';
 for (var i = 0; i < num; i++) {res = ';
return res;
 /** calculate the number of JSON object data/function Jsonlen (jsonobj) {var length = 0;
 For (var item in jsonobj) {length++;
return length;
 /** Parse JSON object function * */function Printobj (obj) {//JSON object level depth deep = (typeof (deep) = = ' undefined ')? 0:deep; var html = "array\n";
 Returns HTML HTML + kong (deep) + "(\ n"; 
 var i = 0; JSON object, you cannot use. length to get the number of data, so you need to customize a calculation function var len = typeof (obj) = = ' array '?
 Obj.length:jsonLen (obj); for (var key in obj) {//To determine the data type, if it is an array or an object, the &&jsonlen (Obj[key]) is due to//1, when the value (similar to: email:) is null Hou, typeof (Obj[key]) will take this key as Object type//2, the value of NULL source is that some fields in the database table have no data, query directly into the JSON return to come back if (typeof (Obj[key)) = = ' Array ' | | (typeof (Obj[key]) = = ' object ' && Jsonlen (Obj[key]) > 0)
   {deep = 3;
   HTML + + KONG (deep) + ' [' + key + '] => ';
   Recursive call this function html = printobj (obj[key],deep); Deep-= 3;
  }else{HTML + KONG (deep + 3) + ' [' + key + '] => ' + obj[key] + ' \ n ';
  } if (i = = len-1) {html + KONG (deep) + ") \ n";
  };
 i++;
} return HTML;
 /** Append print JSON data to HTML page/function P_obj (Obj) {var div = document.getElementById (' print-json-html ');
 if (div!= null) {document.body.removeChild (div);
 };
 var node = document.createelement ("div");//Create a div tag node.id = ' print-json-html ';
 node.innerhtml = ' <pre> ' + printobj (obj) + ' </pre> ';
Document.body.appendChild (node); }

JS Print array length function of multidimensional array

Gets the number of multidimensional arrays
  function Getarrnum (arr) {

    var elenum = 0;

    if (arr = = null) {return

      0;

    }

    for (var i = 0; i < arr.length. i++) {for

      (var j = 0; J < Arr[i].length; J +) {

        elenum++;

      }

    }

    document.write (elenum);

  }

The above commonly used native JS Custom Function Summary is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.