Common native js custom functions summary js get date functions
// Obtain the current time and Date function CurentTime () {var now = new Date (); var year = now. getFullYear (); // year var month = now. getMonth () + 1; // month var day = now. getDate (); // var hh = now. getHours (); // var mm = now. getMinutes (); // minute var clock = year + "-"; if (month <10) clock + = "0"; clock + = month + "-"; if (day <10) clock + = "0"; clock + = day + ""; if (hh <10) clock + = "0 "; clock + = hh + ":"; if (mm <10) clock + = '0'; clock + = mm; return (clock );}
Js get time difference function
// Obtain the time difference of days function getLastTime () {var startTime = new Date (""); // start time var endTime = new Date (); // End Time var lastTime = endTime. getTime ()-startTime. getTime () // The number of milliseconds in the time difference // calculate the number of different days var days = Math. floor (lastTime/(24*3600*1000) // calculated hours var leave1 = lastTime % (24*3600*1000) // The number of milliseconds remaining after the calculation of the number of days var hours = Math. floor (leave1/(3600*1000) // calculate the number of different minutes var leave2 = leave1 % (3600*1000) // The number of milliseconds remaining after the computing hours var minutes = Math. floor (leave2/(60*1000) // calculates the difference in seconds var leave3 = leave2 % (60*1000) // calculates 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 automatically refreshes the page only once
// Stop refreshing window. onload = function () {if (location. search. indexOf ("? ") =-1) {location. href + = "? Myurl ";} else {if (location. search. indexOf (" myurl ") =-1) location. href + =" & myurl ";}}
Ajax instance
$.ajax({ type: "POST", url: "join.php", data: dataString, success: function(){ $('.success').fadeIn(200).show(); $('.error').fadeOut(200).hide(); } });
Get window size in real time
$(window).resize(function(){ var Height = $(window).height(); var Width = $(window).width();})
Js cyclic execution function and timed execution function
// Execute cyclically. showalert () window is executed every three seconds. setInterval (showalert, 3000); function showalert () {alert ("loop execution");} // scheduled execution, execute show () window in 5 seconds. setTimeout (show, 5000); function show () {alert ("scheduled execution ");}
Js get the 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]} the array to be printed by arr * @ param {[type]} space controls the indent of printing * @ param {[type]} space2 controls the indentation of printing 2 */function print_arr (arr, space, space2) {space = space | ''; space2 = space2 |''; var str = "Array
"+ Space + "(
"; For (var I = 0; I" + print_arr (arr [I], space + '', space2 + '');} else {str + = space2 + '[' + I + "] =>" + arr [I] +"
";}} Str + = space + ")
"; Document. write (str );}
Js prints json data into arrays and outputs the data in html
/** Output space function */function blank (num) {var res = ''; for (var I = 0; I <num; I ++) {res + = '';} return res;}/** calculate the data count of the JSON object */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"; // returned HTML + = kong (deep) + "(\ n"; var I = 0; // JSON object, which cannot be used. length is used to obtain the number of data. Therefore, you need to customize a calculation function var len = typeof (obj) = 'array '? Obj. length: jsonLen (obj); for (var key in obj) {// determines the data type. If it is an array or object, it is recursive. // when determining the object type, & jsonLen (obj [key]) is caused by // 1, value (similar to: When email :) is null, typeof (obj [key]) the key will be treated as the object type // 2. The source with the null value is that some fields in the database table do not have data, if (typeof (obj [key]) = 'array' | (typeof (obj [key]) = 'object' & jsonLen (obj [key])> 0) {deep + = 3; html + = kong (deep) + '[' + key + '] =>'; // recursively 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 JSON data to the HTML page */function p_Obj (obj) {var p = document. getElementById ('print-json-html '); if (p! = Null) {document. body. removeChild (p) ;}; var node = document. createElement ("p"); // create a p tag node. id = 'print-json-html '; node. innerHTML ='' + printObj(obj) + '
'; Document. body. appendChild (node );}
Js prints the array length function of multi-dimensional 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 );}