Small JS framework veryide. librar source code

Source: Internet
Author: User

CopyCode The Code is as follows :/*
* Copyright veryide, 2007-2008
* Http://www.veryide.com/
*
* $ ID: veryide. Library. JS, v2.3 Leilei $
*/

Function $ (OBJ) {return document. getelementbyid (OBJ );}
Function $ n (OBJ) {return document. getelementsbyname (OBJ );}
Function $ T (OBJ) {return document. getelementsbytagname (OBJ );}

Function $ V (STR) {document. Write (STR );}

// Body load
Function addloadevent (func ){
VaR oldonload = Window. onload;
If (typeof window. onload! = 'Function '){
Window. onload = func;
} Else {
Window. onload = function (){
Oldonload ();
Func ();
}
}
}

// Object event
Function addobjectevent (Ele, EVT, func ){
VaR oldonevent = ele ['on' + EVT];
If (typeof ele ['on' + EVT]! = 'Function '){
Ele ['on' + EVT] = func;
} Else {
Ele ['on' + EVT] = function (event ){
Oldonevent (event );
Func (event );
}
}
}

// Key event
Function addkeyevent (Key, func ){
If (! Veryide. tmpkey ){
Veryide. tmpkey = [];
}

Veryide. tmpkey ["K" + key] = func;
}

Addkeyevent. Listener = function (E, test ){
VaR event = E | window. event;
If (veryide. tmpkey ["K" + event. keycode]) {
Veryide. tmpkey ["K" + event. keycode] (event );
}
If (TEST ){
Alert (event. keycode );
}
}
// Onkeydown determines charcode/keycode

// Obtain the object
Function GetObject (o ){
If (typeof (o )! = "Object "){
VaR o = $ (O );
}
Return O;
}

/*
String. Prototype. Trim = function (){
Return this. Replace (/(^ \ s +) | \ s + $/g ,"");
}
*/

String. Prototype. Trim = function (){
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}
String. Prototype. ltrim = function (){
Return this. Replace (/(^ \ s *)/g ,"");
}
String. Prototype. rtrim = function (){
Return this. Replace (/(\ s * $)/g ,"");
}

// Returns the number of bytes of the string.
String. Prototype. Long = function (){
VaR I;
VaR L = This. length;
VaR Len;
Len = 0;
For (I = 0; I <L; I ++ ){
If (this. charcodeat (I)> 255)
Len + = 2;
Else
Len ++;
}
Return Len;
}

// Check whether a value exists in the array
Function inarray (A, v ){
VaR L = A. length;
For (VAR I = 0; I <= L; I ++ ){
If (A [I] = V) return true;
}
Return false;
}

// All elements whose class is *
Document. getelementsbyclassname = function (){
VaR children = Document. getelementsbytagname ('*') | document. All;
VaR elements = new array ();
VaR Len = children. length;

For (VAR I = 0; I <Len; I ++ ){
VaR child = children [I];
VaR classnames = Child. classname. Split ('');
For (var j = 0; j <classnames. length; j ++ ){
For (var k = 0; k <arguments. length; k ++ ){
If (classnames [J] = arguments [k]) {
Elements. Push (child );
Break;
}
}
}
}

Return elements;
}

// All elements whose IDs start *
Document. getelementsbyprefix = function (prefix ){
VaR children = Document. getelementsbytagname ('*') | document. All;
VaR elements = new array ();
VaR Len = children. length;

For (VAR I = 0; I <Len; I ++ ){
VaR child = children [I];
VaR eid = Child. ID;
If (Eid & EID. indexof (prefix)>-1)
Elements. Push (child );
}

Return elements;
}

/* Search for the page meta */
Function getmeta (name, ATT ){
Required AGS = Document. getelementsbytagname ("meta ");
For (CNT = 0; CNT <symbol AGS. length; CNT ++ ){
If (cmdags [CNT]. getattribute ("name") = Name ){
If (! ATT ){
Return required AGS [CNT];
} Else {
Return destination AGS [CNT]. getattribute (ATT );
}
} Else {
Return NULL;
}
}
}

// Load the new script
Function loadscript (SRC, target ){
If (! Target ){
VaR root = $ T ("head") [0];
} Else {
VaR root = GetObject (target );
}

VaR script = Document. createelement ("script ");
Script. type = "text/JavaScript ";
Script. src = SRC;

VaR code = arguments [2];
If (CODE ){
Script. charset = code;
}

Root. appendchild (SCRIPT );
}

// Get the file name
Function getfilename (URL ){
VaR strurl = location. href;
If (URL ){
Strurl = URL;
}
VaR arrurl = strurl. Split ("/");
VaR strpage = arrurl [arrurl. Length-1];
Return strpage;
}

// Absolute object location
Function getposition (OBJ ){
VaR OBJ = GetObject (OBJ );

This. width = obj. offsetwidth;
This. Height = obj. offsetheight;
This. Top = obj. offsettop;
This. Left = obj. offsetleft;
While (OBJ = obj. offsetparent ){
This. Top + = obj. offsettop;
This. Left + = obj. offsetleft;
}
}

Function getselect (OBJ ){
VaR OBJ = GetObject (OBJ );

This. value = "";
This. Text = "";
This. Index = "";

If (obj. length> 0 ){
This. value = OBJ [obj. selectedindex]. value;
This. Text = OBJ [obj. selectedindex]. text;
This. Index = obj. selectedindex;
}
This. getatt = function (ATT ){
Return OBJ [obj. selectedindex]. getattribute (ATT );
}
}

//
Function getradio (OBJ ){
VaR OBJ = $ n (OBJ );
VaR Len = obj. length;

This. value = "";
For (VAR I = 0; I <Len; I ++ ){
If (OBJ [I]. Checked = true ){
This. value = OBJ [I]. value;
Break;
}
}
}

// Hide object display
Function showhide (OBJ ){
VaR OBJ = GetObject (OBJ );
If (OBJ ){
If (obj. offsetheight = 0 | obj. style. display = "NONE") {obj. style. display = "Block";} else {obj. style. display = "NONE ";}
}
}

Function setcheckbox (OBJ, v ){
VaR OBJ = $ n (OBJ );

If (OBJ [0]) {
VaR Len = obj. length;
For (VAR I = 0; I <Len; I ++ ){
If (OBJ [I]. value = V ){
OBJ [I]. Checked = true;
} Else {
OBJ [I]. Checked = false;
}
}
} Else {
OBJ = $ (OBJ );
If (obj. value = V ){
OBJ. Checked = true;
} Else {
OBJ [I]. Checked = false;
}
}
}

// Select a list item
Function setselect (OBJ, v ){
VaR OBJ = $ (OBJ );
VaR Len = obj. length;
For (VAR I = 0; I <Len; I ++ ){
If (OBJ [I]. value = V ){
OBJ. selectedindex = I;
Break;
}
}
}

// Select a single option
Function setradio (O, v ){
VaR OBJ = $ n (O );
VaR Len = obj. length;
For (VAR I = 0; I <Len; I ++ ){
If (OBJ [I]. value = V ){
OBJ [I]. Checked = true;
Break;
}
}
}

// Disable the object
Function setdisabled (OBJ, B ){
VaR OBJ = GetObject (OBJ );
If (OBJ) {obj. Disabled = B ;}
}

Function setclass (OBJ, class, type ){
VaR OBJ = GetObject (OBJ );
If (OBJ ){
Switch (type ){
Case "+ ":
OBJ. classname + = "" + class;
Break;

Case "-":
OBJ. classname = obj. classname. Replace (class ,"");
Break;

Case "":
OBJ. classname = Class;
Break;

}
}
}

// Create a continuous downlink list item
Function newnumoption (OBJ, S, E, T ){
VaR OBJ = GetObject (OBJ );
If (t = "new") {obj. Length = 0 ;}
For (VAR I = s; I <(E + 1); I ++ ){
OBJ. Options [obj. Length] = New Option (I, I );
}
}

// Delete an object
Function delelement (OBJ ){
VaR OBJ = GetObject (OBJ );
VaR P = obj. parentnode;
P. removechild (OBJ );
}

// Confirm the operation
Function getconfirm (Info ){
If (! Confirm (Info) {return false}
}

// Open a link in a new window
// Rel = "_ blank"
Function _ blank (){
VaR anchors = Document. getelementsbytagname ("");
VaR Len = anchors. length;
For (VAR I = 0; I <Len; I ++ ){
VaR anchor = anchors [I];
If (anchor. getattribute ("href") & anchor. getattribute ("rel") = "_ blank "){
Anchor.tar get = "_ blank ";
}
}
}

// Obtain URL parameters
Function getquery (n, URL ){
If (! URL ){
VaR url = location. href;
}

VaR v = '';
VaR o = URL. indexof (n + "= ");
If (o! =-1 ){
O + = n. Length + 1;
E = URL. indexof ("&", O );
If (E =-1 ){
E = URL. length;
}
V = Unescape (URL. substring (o, e ));
}

// Seo
If (! V ){
VaR o = URL. indexof (n + "-");
If (o! =-1 ){
O + = n. Length + 1;
E = URL. indexof ("-", O );
If (E =-1 ){
E = URL. length;
}
V = Unescape (URL. substring (o, e ));
}
}

Return V;
}

// Convert the fullwidth to halfwidth
Function switchchar (STR ){
VaR str1 = "1234567890 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";
VaR str2 = "1234567890 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";

VaR Len = Str. length;
For (VAR I = 0; I <Len; I ++ ){
VaR n = str2.indexof (Str. charat (I ));
If (n! =-1) STR = Str. Replace (Str. charat (I), str1.charat (n ));
}
Return STR;
}

/*
Obtain random characters
***********
LEN Length
Upper case letter for vupper
Whether vlower is a lowercase letter
Number of vnum
*/
Function getrnd (Len, vupper, vlower, vnum ){
VaR seed_array = new array ();
VaR seedary;

Seed_array [0] = ""
Seed_array [1] = "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 ";
Seed_array [2] = "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 ";
Seed_array [3] = "0 1 2 3 4 5 6 7 8 9 ";

If (! Vupper &&! Vlower &&! Vnum) {vupper = true; vlower = true; vnum = true ;}

If (vupper) {seed_array [0] + = seed_array [1];}
If (vlower) {seed_array [0] + = "" + seed_array [2];}
If (vnum) {seed_array [0] + = "" + seed_array [3];}

Seed_array [0] = seed_array [0]. Split ("");
Seedary = "";

For (VAR I = 0; I <Len; I ++ ){
Seedary + = seed_array [0] [math. Round (math. Random () * (seed_array [0]. Length-1)]
}
Return (seedary );
}

/*
Obtain cookies
Name Cookie name
Sub-Cookie name
*/
Function getcookie (name, sub ){
VaR STR = "";
VaR arr = document. cookie. replace (/% 25/g, "% "). replace (/% 5f/g ,"_"). match (New Regexp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (Arr! = NULL ){
Try {
STR = decodeuricomponent (ARR [2]);
} Catch (e ){
STR = arr [2];
}
}

If (sub ){
If (STR ){
VaR nsubvaluebegin = Str. indexof (sub + "= ");
} Else {
VaR nsubvaluebegin =-1
}

If (nsubvaluebegin! =-1 ){
VaR nsubvalueend = Str. indexof ("&", nsubvaluebegin );
If (nsubvalueend =-1)
Nsubvalueend = Str. length;
VaR ssubvalue = Str. substring (nsubvaluebegin + sub. Length + 1, nsubvalueend); // obtain the specified subkey Value
STR = ssubvalue;
} Else {
STR = "";
}
}
Return STR;
}

Function setcookie (Key, value, iexpiredays, domain ){
VaR cookies = key. Replace ("_", "% 5f") + "=" + encodeuricomponent (value) + ";";

If (iexpiredays ){
VaR dexpire = new date ();
Dexpire. settime (dexpire. gettime () + parseint (iexpiredays * 24*60*60*1000 ));
Cookies + = "expires =" + dexpire. togmtstring () + ";";
}

If (domain ){
Cookies + = "Domain =" + domain + ";";
}
Cookies + = "Path = /;";
Document. Cookie = cookies;
}

/* State */
Veryide. Script ["library"] = true;

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.