Common JS verification and summary of common methods

Source: Internet
Author: User
Tags add time rtrim time in milliseconds

Below are some common JS methods used in the work, which are summarized to facilitate future Viewing:

// General method for closing a browser window without a closed prompt box
Function closewin (){
Window. Opener = NULL;
// Window. Opener = top;
Window. Open ("", "_ Self ");
Window. Close ();
}
// Open the link only in a new window

Function openonewin (urlobj ){
Window. Open (urlobj. href, '1 ','');
Return false;
}

// Open the link in a new window
Function openwin (URL ){
Window. open (URL, '_ blank', 'height = 100, width = 400, Top = 0, Left = 0, toolbar = No, menubar = No, scrollbars = No, resizable = No, location = No, status = no ');

// Height = 100 window height;
// Width = 400 window width;
// Top = the pixel value between the 0 window and the top of the screen;
// Left = 0 the pixel value between the window and the left of the screen;
// Toolbar = No indicates whether to display the toolbar. Yes indicates display;
// Menubar. scrollbars indicates the menu bar and scroll bar.
// Resizable = No whether to change the window size. Yes indicates yes;
// Location = No whether the address bar is displayed. Yes indicates yes;
// Status = No whether to display the information in the status bar (usually the file has been opened), yes is allowed;

}

 

Disable IFRAME in the parent form in IFRAME

Main.html

<HTML>

<Head>

<Title> main form </title>

<SCRIPT type = "text/JavaScript">

$ (Function (){

$ ("# Btnopen"). Click (function () {// open the link in ifram

Openfrm ("div_mask", "divcontainer", "http://www.baidu.com /");

});

 

$ ("# Hidclose"). Click (function () {// close the IFRAME form

Closefrm ("div_mask", "divcontainer ");

});

});

// Parameter: 0-Mask Layer ID; 1-pop-up layer; 2-url address; 3-iframe width; 4-iframee height; (the parameter of height and width is null)

Function openfrm (maskid, divid, URL, W, h ){
VaR _ w = w | 300; // default width and height
VaR _ H = H | 200;
If ($ ("#" + divid + "> # myfrm"). Length = 0) {// If the IFRAME does not exist, it is created and added to the DIV container.
$ ("<IFRAME> </iframe>"). ATTR (

{ID: "myfrm", width: _ w + "PX", height: _ H + "PX", frameborder: "no", marginwidth: "0", marginheight: "0", scrolling: "Yes", allowtransparency: "Yes", border: "0 "}

). Appendto ($ ("#" + divid ));
}
Document. getelementbyid ("myfrm"). src = URL;

Changepopup (maskid, divid); // set the bullet Layer

}

// Parameter: 0-Mask Layer ID; 1-pop-up layer
Function closefrm (maskid, divid ){
$ ("#" + Divid + "> iframe"). Remove (); // remove IFRAME

Changepopup (maskid, divid); // set the bullet Layer

}

// Change the display of the pop-up layer

Function changepopup (maskid, divid ){

If ($ ('#' + maskid).css ("display") = "NONE "){
VaR left = (document. Body. scrollwidth-$ ('#' + divid). Width ()/2;
VaR Top = Document. Body. scrolltop + 100;
$ ('#' + Maskid).css ({display: "Block "});
$ ('#' + Divid).css ({display: "Block", "Left": Left + "PX", "TOP": Top + "PX "});
} Else {
$ ('#' + Maskid).css ({display: "NONE "});
$ ('#' + Divid).css ({display: "NONE "});
}
}

</SCRIPT>

</Head>

<Body>

<Div id = "div_mask" class = "popup_mask"> <! -- Mask layer -->
</Div>

<Input type = "button" id = "btnopen" value = "open link"/>

<Input type = "hidden" id = "hidclose"/> <! -- Hide the close button -->

<Div id = 'diviner iner 'class = "popup_win"> <! -- Pop-up layer for containers that contain IFRAME -->

</Div>

</Body>

</Html>

 

Child.html

<HTML>

<Head>

<Title> subform </title>

<SCRIPT type = "text/JavaScript">

$ (Function (){

$ ("# Btnclose"). Click (function (){

// Trigger the parent form button event
Optional bytes parent.doc ument. getelementbyid ("hidclose"). Click ();

});

});

</Head>

<Body>

<Input type = "button" id = "btnclose" value = "close"/>

<Div>

</Body>

</Html>

 

// Select all \ cancel Effect

Function ckselectall (tblid, ckid) {// select all check box Click Event
$ ("#" + Ckid ). click (function () {$ ("#" + tblid + ": checkbox: Not ([disabled])"). ATTR ("checked", this. checked );});
}

Function ckselect (tblid, ckid) {// single row check box Click Event

$ ("#" + Tblid + ": checkbox [ID! = '"+ Ckid +"'] "). Each (function (){
$ (This). Click (function (){
VaR flag = false;
$ ("#" + Tblid + ": checkbox [ID! = '"+ Ckid +"'] "). Each (function (){
If (! $ (This). ATTR ("checked") {flag = true ;}
});

If (FLAG ){
$ ("#" + Ckid). ATTR ("checked", false );
}
Else {
$ ("#" + Ckid). ATTR ("checked", true );
}
});
});
}

// Text box with automatic height

Function autoheight (OBJ, default_height ){
VaR s_h = obj. scrollheight;
If (s_h> default_height ){
OBJ. style. Height = s_h + "PX ";
} Else {
OBJ. style. Height = default_height + "PX ";
}
}

<Textarea id = "TXT" style = "height: 20px; overflow: hidden;" Cols = '20' onkeyup = "autoheight (this, 20);"> </textarea>

 

// Truncate the string
Function cutstring (STR, Len ){

Len = parseint (LEN) * 2;

VaR newlength = 0;
VaR newstr = "";
VaR chineseregex =/[^ \ x00-\ xFF]/g; // match double-byte characters. The length of one double-byte character ranges from 2 to 1.

// Var chineseregex = new Regexp ("^ [\ u0391-\ uffe5] $"); // difference: Space Mismatch
VaR singlechar = "";
VaR strlength = Str. length;
For (VAR I = 0; I <strlength; I ++ ){
Singlechar = Str. charat (I). tostring ();
If (singlechar. Match (chineseregex )! = NULL ){
Newlength + = 2;
}
Else {
Newlength ++;
}
If (newlength> Len ){
Break;
}
Newstr + = singlechar;
}
Return newstr;
}

 

// Check and set the maximum length of the element value
Function checkmaxlength (O, maxlength ){
VaR Carr = O. value. Match (/[^ x00-xff]/g); // match double byte characters, a dual byte length Meter 2, ASCII character meter 1
VaR length = O. value. Length + (CARR = NULL? 0: Carr. Length );

Len = math. Ceil (length/2 ));

If (LEN> parseint (maxlength )){

// Truncate the string
O. value = cutstring (O. Value, maxlength );
}
}

// Prompt for changing the input text Length
Function changeablelengthtip (txtid, spanid, maxlen ){
VaR Carr = $ ("#" + txtid ). val (). match (/[^ x00-xff]/g); // match double byte characters, a dual Byte Character length Meter 2, ASCII character meter 1
VaR length = obj. value. Length + (CARR = NULL? 0: Carr. Length );
VaR Len = length/2; // calculate the length of two bytes.
VaR ablelen = maxlen-math. Ceil (LEN );
$ ("#" + Spanid). Text (ablelen. tostring ());
}

Function gettextreallength = function (txtid ){
VaR text = $ ("#" + txtid). Val ();
VaR charcode = NULL;

VaR length = 0;
For (VAR I = 0; I <text. length; I ++ ){
VaR J = text. charcodeat (I );
'If (j> 127 ){
Length + = 1;
} Else {
Length ++ = 0.5;
}
Continue;
}
// Calculate the length of the current text
Length = math. Ceil (length );
Return length;
};

 

 

// Use a regular expression to replace spaces with empty strings.
String. Prototype. ltrim = function (){
Return this. Replace (/^ [\ s] +/g ,"");
}
String. Prototype. rtrim = function (){
Return this. Replace (/[\ s] + $/g ,"");
}
String. Prototype. Trim = function (){

// Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
Return this. ltrim (). rtrim ();
}

 

// Extract HTML Tag content

String. Prototype. replacetag = function (tagname ){
If (type of tagname = "string") & (/^ [a-zA-Z0-9] + $/. Test (tagname ))){

VaR Reg = new Regexp ("<" + tagname + "[^>] *> ([^ <] *) </" + tagname + "> ", "Gi ");
Return this. Replace (Reg, "$1 ");
// Var reg1 = new Regexp ("</? "+ Tagname +" [^>] *?> "," Gi ");
// Return this. Replace (reg1 ,"");
} Else {
Return this;
}
}

Example:
VaR STR = "SD <a target = '_ selp'> 1111 </a> asff <a target =' _ selp'> 2222 </a> <A> 3333 </ a> Alert (Str. replacetag ("A"); // output: sd1111asff22223333>

// Note:

// Alert (typeof undefined) = "undefined"); // undefined, true
// Alert (typeof null) = "object"); // object, true
// Alert (typeof "" = "string"); // string, true
// Alert (typeof [] = "object"); // object, true
// Alert (typeof {}= = "object"); // object, true

 

 

// Extract the tag href

C:

RegEx Reg = new RegEx (@"(? Is) <A [^>] *? Href = (['""]?) (? <URL> [^ '"\ s>] +) \ 1 [^>] *> (? <Text> (? :(?! </? A \ B).) *) </a> ");
Match m = reg. Match (HTML );

String url = M. Groups ["url"]. value;

 

// Get the page address for passing Parameters

String. Prototype. getquerystring = function (name ){
VaR Reg = new Regexp ("(^ | & | \\?) "+ Name +" = ([^ &] *) (& | $) "), R;
If (r = This. Match (REG) return Unescape (R [2]);
Return NULL;
};

Function getparameters (){
VaR STR = location. Search. substr (1)
VaR Reg =/([^ & =] *) = ([^ &] *) (? = & | $)/G
VaR arr;
VaR Results = new array ();
While (ARR = reg.exe C (STR )){
Results [arr [1] = arr [2];
}
Return results;
}

Function getsearchasarray (srchstr ){
VaR Results = new array ();
VaR input = Unescape (srchstr. substr (1 ));
If (input ){
VaR srcharray = input. Split ("&");
VaR temparray = new array ();
For (VAR I = 0; I <srcharray. length; I ++ ){
Temparray = srcharray [I]. Split ("= ");
Results [temparray [0] = temparray [1];
}
}
Return results;
}

Function isurl (str_url) {// verify the URL
VaR strregex = "^ (HTTPS | HTTP | FTP | RTSP | MMS )? ://)"
+ "? ([0-9a-z _!~ * '(). & =+ $ %-] + :)? [0-9a-z _!~ * '(). & =+ $ %-] + @)? "// FTP user @
+ "([0-9] {1, 3} \.) {3} [0-9] {1, 3}" // URL in IP Format-199.194.52.184
+ "|" // Allow IP addresses and domain names)
+ "([0-9a-z _!~ * '()-] + \.) * "// Domain name-www.
+ "([0-9a-z] [0-9a-z-] {0, 61 })? [0-9a-z] \. "// second-level domain name
+ "[A-Z] {2, 6})" // first level domain-. com or. Museum
+ "(: [0-9] {1, 4 })? "// Port-: 80
+ "((/?) | "// A slash isn' t required if there is no file name
+ "(/[0-9a-z _!~ *'().;? : @ & =+ $, % #-] +) + /?) $ ";
VaR Re = new Regexp (strregex );
Return re. Test (str_url );
}

 

1. Verify positive and negative integers and Decimals

VaR Reg =/^ -? \ D + (\. \ D + )? $ /;

2. Verify Chinese

VaR Reg =/[\ u4e00-\ u9fa5]/g;

3. Verification time

A) Time:

VaR regexobj =/^ (2 [0-3]) | ([0-1]? [0-9]): [0-5] [0-9] $ /;

VaR regexobj =/^ (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D $ /;

B) year, month, and day (including the leap year, leap month judgment ):

Regexobj =/^ (1 [6-9] | [2-9] \ D) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ D) \ D {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ D) \ D {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ D) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ /;

 

// Scroll to the bottom of the event

$ ("# Div_scroll" ).css ("height", $ (window). Height ()-150). Scroll (function (){
VaR s_h = This. scrollheight; // The total length of the scroll distance (note that it is not the length of the scroll bar)
VaR s_top = This. scrolltop; // The current position to which the image is scrolled.
VaR H = $ (this). Height (); // Div height
If (H + s_top = s_h ){

// Event processing when scrolling to the bottom

}

}

 // Obtain the timestamp

Method 1: var timestamp = date. parse (new date (); // display the time in milliseconds as 000
Method 2: var timestamp = (new date (). valueof (); // obtain the timestamp of the current millisecond
Method 3: var timestamp = new date (). gettime (); // obtain the timestamp of the current millisecond

 

// Add time

Date. Prototype. adddays = function (number ){
If (! Isnan (number )){
VaR nowtime = This. gettime ();
Return new date (nowtime + (number) * 24*60*60*1000 );
} Else {
Return this;
}
};

 

// Time format

Date. Prototype. format = function (Format)
{
VaR o = {
"M +": This. getmonth () + 1, // month
"D +": This. getdate (), // day
"H +": This. gethours (), // hour
"M +": This. getminutes (), // minute
"S +": This. getseconds (), // second
"Q +": Math. Floor (this. getmonth () + 3)/3), // quarter
"S": This. getmilliseconds () // millisecond
};
If ("/(Y +)/". Test (Format )){

Format = format. Replace (Regexp. $1, (this. getfullyear () + ""). substr (4-Regexp. $1. Length ));

}
For (var k in O ){

If (New Regexp ("(" + K + ")"). Test (Format )){
Format = format. Replace (Regexp. $1, Regexp. $1. Length = 1? O [k] :( "00" + O [k]). substr ("" + O [k]). Length ));

}

}
Return format;
}

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.