Miscellaneous notes: JavaScript, jQuery, and ECMAscript

Source: Internet
Author: User

Get the key name of the object:

var arr={a:12,b:34,c:534,d:3344}; var srr=[]; var i=0;  for (var in arr) {srr[i]=items;i++;} // srr=["A", "B", "C", "D"];

jquery Action Cookie

$.cookie (' name ', ' value ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true});

jquery blocks hyperlink execution, Button equals effect

$ ('. a '). Click (function(e) {E.preventdefault ();});

jquery selector: name^= ' AAA ' is an AAA for the start of the search, matching AAA, Aaab, AAAACD, aaa_etc

jquery (haha). Find ("input[title],select[title][name^= ' Data_ '],textarea[title]") jquery (haha). Find (" INPUT[TYPE=HIDDEN]:GT (5): LT (19) ")

Array conversion string

var arr = ["A", "B", "C", "D", "E" ]; var ss=arr.join (","); SS= "A, B, C, D, E"

JavaScript generates a fixed-length 62 binary random code (0-9, A-Z, A-Z)

function getrandom (param) {str=[  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "Z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" = "";
   for (i=0;i<param;i++//1-62//Generate PHP random number } return key;} Getrandom (32);  

JavaScript copies the object, the direct assignment is just the reference address, and the modification of one other has an effect, and if not, the new object (obj) is used to generate the

var s1={name:1}; var s2=S1; // s2={name:1},s1={name:1}; s1.name=2; S1={name:2},s2={name:2}; // S1 S2 point to the same address var s1={name:1}; var s2=New  Object (S1); // s2={name:1},s1={name:1}; s1.name=2; // s2={name:1},s1={name:1};

String substitution, Repalce can only change the first, if you want to change all to use the regular

var string = "John John"; Console.log (String.Replace (//  "Joana John"// "Joana Joana"

Stringify translating JavaScript objects into JSON strings

Parse converts a JSON string into a JavaScript object, using eval () If parse does not work, but Eval is risky and performs function

Json.stringify (); Json.parse ();

Listening for keyboard events: some of the tables below

ESC 27A 65B 66...Z all shift 16ctrl 17alt 91 win
function (e) {    //dosth})

JavaScript determines whether an element is in an array (has, returns index, no, returns-1)
function In_array (str, arr) {    = arr.length    for (var i = 0; i < length; i++  {        if (str = = = Arr[i])            {return  i;        }    }     return -1;}

SetTimeout will open a new thread, and sometimes you can listen to it directly

 Do {}while (jQuery ("#flow_id option"). length==0)

window.open, open a new page, this new page can be opened by JS to turn off

Parameter table:

Height High
Width Wide
Top From the top of the screen
Left From the left of the screen
Toolbar Tool bar
MenuBar Menu bar
ScrollBars scroll bar
Resizable Adjust size
Location Address bar
Status Status sample
window.open (' page.html ', ' newwindow ', ' Height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, Resizable=no,location=no, Status=no ')

IE5 position:fixed; Be sure to use the old standard HTML document. Otherwise the fixed failure.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Old IE Analog placeholder:

<name= "name"  ID= "name"  onfocus= "if (value = =" Student name ') {value = '} ' onBlur= ' if (value = = ') {value= ' student name '} '/>

JS counter, and then each time directly C1 (), it can be counted.
function Create_a_counter () {var count = 0; return function  = Count +1; return count;}} var c1=create_a_counter ();

The above counters improve the code, which is structured:

function counter () {    var count = 0;     function Doplus () {        count=count+1;  return  count;    }     function Dominus () {        = count-1;  return  count;    }     var publicapi={        plus:doplus,minus:dominus    }    return  Publicapi;}

Dynamic loading JS code:

function Insertjs (strURL, fonload) {    var script = document.createelement ("script");     = "Text/javascript";     = strURL;     = "UTF-8";     if (Fonload) {script.onload = fonload;}    Document.body.appendChild (script);} Insertjs ("http://xxx.com/js/test.js");

JS implementation of the text beyond the partial use of ellipses instead

functioncutstring (str, len) {//the Length property reads 1 Chinese characters .   if(Str.length*2 <=Len) {      returnstr; }   varstrlen = 0; vars = "";  for(vari = 0;i < str.length; i++) {s= S +Str.charat (i); if(Str.charcodeat (i) > 128) {strlen= strlen + 2; if(Strlen >=Len) {            returnS.substring (0,s.length-1) + "..."; }      } Else{strlen= strlen + 1; if(Strlen >=Len) {            returnS.substring (0,s.length-2) + "..."; }      }   }   returns;}

Specific implementation

function jcutstring (obj, limit) {    var len = obj.length;      for (i=0;i<len; i++) {        obj.eq (i). Text (cutstring (Obj.eq (i). Text (), limit));}    }; $ (document). Ready (function() {    var href = $ (". AAA");     ();});

Window.haha is an object, and in chrome the console hits Haha and Window.haha comes out with the definition of function haha. So the following three lines are actually the same, you can call the function dynamically in this way
haha (); Window.haha (); window["haha"] ();

JS Check if JS CSS is loaded

function Isinclude (name) {    var js=/js$/i.test (name);     var es=document.getelementsbytagname (js? ') Script ': ' link ');      for (var i=0;i<es.length;i++)     if (Es[i][js? ') src ': ' href '].indexof (name)!=-1)returntrue;     return false ;}

Cannot right-click with selected Web page workaround:

Copy the following code into the address bar:

javascript:document.oncontextmenu=New Function ("event.returnvalue=true;"); document.onselectstart=New Function ("event.returnvalue=true;");

JQuery HTML Escape

Jquery.parsehtml (CNT)

JS to reorder, the sequence of the fixed range, the efficiency is good.

function Notsort (list) {   var haha=[],result=[];     for (var i=0;i<list.length;i++) {      Haha[list[i]]=list[i];   }     for (var i=0;i) {      if(Haha[i]) {         Result.push (haha[i ]);      }   }    return result;}

Miscellaneous: JavaScript, JQuery, and ECMAscript

Related Article

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.