Small JS frame Veryide.librar source code _js Object-oriented

Source: Internet
Author: User
Tags cookie names
Copy Code code as follows:

/*
* Copyright veryide,2007-2008
* http://www.veryide.com/
*
* $Id: veryide.library.js,v2.3 17:29 2008-11-16 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 Judge Charcode/keycode

Get 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 string bytes
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 if a value exists within 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;
}

Class is all elements of *
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 with ID beginning with *
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;
}

/* Find page meta*/
function Getmeta (Name,att) {
MetaTags = document.getElementsByTagName ("meta");
for (cnt = 0; cnt < metatags.length; cnt++) {
if (Metatags[cnt].getattribute ("name") ==name) {
if (!att) {
return metatags[cnt];
}else{
return Metatags[cnt].getattribute (ATT);
}
}else{
return null;
}
}
}

Load 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 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;
}

Object Absolute Position
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
}
}
}



Object Display hidden
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 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 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
}
}
}

disabling objects
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

}
}
}

New consecutive down-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 Object
function Delelement (obj) {
var obj=getobject (obj);
var P=obj.parentnode;
P.removechild (obj);
}

Confirm Action
function Getconfirm (info) {
if (!confirm (info)) {return false}
}

New window open link
Rel= "_blank"
function _blank () {
var anchors = document.getElementsByTagName ("A");
var len = anchors.length;
for (var i=0; i<len; i++) {
var anchor = Anchors[i];
if (Anchor.getattribute ("href") && anchor.getattribute ("rel") = = "_blank") {
Anchor.target = "_blank";
}
}
}

Get 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;
}

Full angle Turn half angle
function Switchchar (str) {
var str1= "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var str2= "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

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;
}

/*
Get random characters
***********
Len length
Vupper is uppercase
Vlower whether lowercase letters
Vnum whether the number
*/
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);
}


/*
Get cookies
Name Cookie Names
Sub Child 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);//Get 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.