Some common functions of JS function code _javascript Skills

Source: Internet
Author: User
Copy Code code as follows:

Get Object
function GetObject (objectid,top)
{
doc = top?window.top.document:document;
if (typeof (ObjectId)!= "Object" && typeof (ObjectId)!= "function")
{
if (Doc.getelementbyid && Doc.getelementbyid (objectId))
{
The Consortium DOM
Return Doc.getelementbyid (OBJECTID);
}
else if (Doc.getelementsbyname (objectId))
{
Return Doc.getelementsbyname (OBJECTID) [0];
}
else if (Doc.all && doc.all (objectId))
{
MSIE 4 DOM
Return Doc.all (OBJECTID);
}
else if (doc.layers && Doc.layers[objectid])
{
NN 4 DOM. Note:this won ' t find nested layers
return Doc.layers[objectid];
}
Else
{
return false;
}
}else
return objectId;
}
Get relative path
function Getrelativepath ()
{
var url = location.href;//Current URL
var Urlcs = String (Location.search);
url = url.replace (Urlcs, "");
var path = Url.length-url.replace (/\//g, ""). length-3; The level is the URL contains/length-no included/length minus item Header/number

var str = "";
for (var i = 0; i < path; i++)
{
str + = ". /";//The string combined into a relative path returns
}
return str;
}
Load other JS files or CSS files
function Loadjscssfile (filename,filetype,chkonce)
{
filetype =!filetype? " JS ": filetype;
var had = false;
if (filetype== "JS")
{
if (chkonce)
{
var allscripts = document.getelementsbytagname ("script");
for (Var i=0;i<allscripts.length;i++)
{
try{
if (allscripts[i].src.indexof (filename) >-1)
{
had = true;
Break
}
}catch (e) {}
}
}
if (!had)
{
document.write ("<script src=" "+filename+" "src=" "+filename+" "type= ' Text/javascript ' ></script>");
}
}else
{
if (chkonce)
{
var allcss = document.getelementsbytagname ("link");
if (allcss.length)
{
for (Var i=0;i<allcss.length;i++)
{
try{
if (allcss[i].href.indexof (filename) >-1)
{
had = true;
Break
}
}catch (e) {}
}
}
}
if (!had)
{
document.write ("<link type= ' text/css ' rel= ' stylesheet ' href= '" "+filename+" "href=" "+filename+" "/>");
}
}
}
Defining the root directory path
var Root_path = Getrelativepath ();
var js_path = root_path+ ' js/';
var ajax_path = root_path+ ' ajax/';
var css_path = root_path+ ' css/';
var images_path = root_path+ ' images/';
var editor_path = root_path+ ' uploadeditor/';
var pub_path = root_path+ ' uploadfile/';

To load a public variable JS
Loadjscssfile (js_path+ "globalpara.js");

Set an item in the Drop-down list to be selected
function Setseloption (OBJID,VLU)
{
ObjID = GetObject (ObjID);
for (Var i=0;i<objid.options.length;i++)
{
if (OBJID.OPTIONS[I].VALUE==VLU)
{
Objid.options[i].setattribute ("Selected", "Selected");
Break
}
}
}
Set an item to be selected according to the option text in the Drop-down table
function Settxtoption (objid,txt)
{
ObjID = GetObject (ObjID);
for (Var i=0;i<objid.options.length;i++)
{
if (objid.options[i].innerhtml==txt)
{
Objid.options[i].setattribute ("Selected", "Selected");
Break
}
}
}

Set an item in a radio button group to be selected
function Setselradio (OBJNAME,VLU)
{
objname = Document.getelementsbyname (objname);
for (Var i=0;i<objname.length;i++)
{
if (OBJNAME[I].VALUE==VLU)
{
Objname[i].setattribute ("Checked", "checked");
Break
}
}
}

Set some items in the check box by ID to be selected
The VLU format is: 1,2,3
function Setselcheckbox (PREFIX,VLU)
{
var _arr = Vlu.split (",");
if (_arr!= "")
{
for (var i=0; i<_arr.length; i++)
{
GetObject (Prefix+_arr[i]). checked = true;
}
}
}

Shortcut input, Fromobj: source object, Toobjid: innerHTML value of Toobjid when the target object's id,txt is True
function Fastinput (fromobj,toobjid,txt)
{
if (fromobj.value== ' | | | fromobj.value==0) return FALSE;
txt =!txt? False:txt;
var toobj = GetObject (Toobjid);
if (TXT)
{
if (typeof (toobj.value) = = ' undefined ')
toobj.innerhtml = Totxt (fromobj.options[fromobj.selectedindex].innerhtml);
Else
Toobj.value = Totxt (fromobj.options[fromobj.selectedindex].innerhtml);
}else
{
if (typeof (toobj.value) = = ' undefined ')
toobj.innerhtml = Fromobj.value;
Else
Toobj.value = Fromobj.value;
}
}

/*
IE6,IE7 preview pictures before uploading pictures
IE6 can also detect the size of the picture at the same time
Size Unit is KB
<style type= "Text/css" ><!--

. Newpreview
{
width:400px; height:300px;
FILTER:progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=scale)
}
. Nodis{display:none;}
--></style><style type= "Text/css" bogus= "1" >
. Newpreview
{
width:400px; height:300px;
FILTER:progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=scale)
}
. Nodis{display:none;} </style>
<input type= "File" Name= "Litpic" onchange= "previewimg" (This, ' Newpreview ', ' Ndspreview ',) "/>
<div id= "Newpreview" class= "Newpreview" ></div>

*/
function Previewimg (imgfile,newpreview,ndspreview,size)
{
Newpreview = GetObject (Newpreview);
if (!imgfile | |!imgfile.value | |!newpreview) {return};
var patn =/\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$/i;
if (Patn.test (Imgfile.value))
{
Try{newpreview.filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src = imgfile.value; catch (e) {}
if (navigator.appVersion.indexOf ("MSIE 6.0", 0) >-1)
{
Size=!size?0:size;
Ndspreview = GetObject (Ndspreview);
if (Ndspreview)
{
Try{ndspreview.attachevent ("onReadyStateChange", function () {checkimgsize (ndspreview,size)})}catch (e) {}
NDSPREVIEW.SRC = Imgfile.value;
}
}
}
Else
{
Alert ("You are not selecting an image file, please choose again.");
}
}
function Previewimgnow (imgdiv,imgfile)
{
Try{getobject (Imgdiv). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src = Imgfile}catch (e) {}
}
Check the size of the picture before uploading
The condition is to trigger the function to display the thumbnail on img when file changes
Size Unit is KB
function Checkimgsize (img,size)
{
img = GetObject (IMG);
if (img.readystate = = "complete")
{
var limit = size * 1024;
if (Img.filesize > Limit)
{
Alert ("Error!") You upload the picture size is "+ (parseint (img.filesize/1024)) +" KB, exceeding the "+size+" KB limit, please upload ");
return False
}else
return true;
}
return true;
}
Detecting the client environment
function Clientmentinfo ()
{
var me = this;
var appver = navigator.useragent;

This. Getbrowsername = function () {
if (appVer.indexOf ("MSIE") >0) return "IE";
else if (appVer.indexOf ("Firefox") >0 return "Firefox";
else if (appVer.indexOf ("Chrome") >0) return "Chrome";
else if (appVer.indexOf ("Safari") >0 return "Safari";
else if (appVer.indexOf ("Camino") >0) return "Camino";
else if (appVer.indexOf ("Konqueror") >0) return "Konqueror";
else return "other";
}

This. Getosinfo = function () {
var _pf = Navigator.platform;
if (_PF = = "Win32" | | _pf = = "Windows")
{

if (appVer.indexOf ("Windows NT 6.0") >-1 | | | appver.indexof ("Windows Vista") >-1)
{
Return ' Windows Vista ';
}else if (appVer.indexOf ("Windows NT 6.1") >-1 | | appver.indexof ("Windows 7") >-1)
{
Return ' Windows 7 ';
}else
{
try{
var _winname = Array (' Watts ', ' XP ', ' 2003 ');
var _ntnum = Appver.match (/windows NT 5.\d/i). toString ();
Return ' Windows ' + _winname[_ntnum.replace (/windows NT 5. ( \d)/I, "$")];
}catch (e) {return ' Windows ';}
}
}else if (_PF = "mac68k" | | _pf = = "MACPPC" | | _pf = "Macintosh")
{
return "Mac";
}else if (_PF = = "X11")
{
return "Unix";
}else if (String (_PF). IndexOf ("Linux") >-1)
{
return "Linux";
}else
{
return "Unknow";
}
}

This. OS = Me. Getosinfo (); Operating system Type
This. Iever = null;
This. Bs_lang = (navigator.appname = ' Netscape '? navigator.language:navigator.browserLanguage);//Browser language version
This. Bs_name = Me. Getbrowsername (); Browser name

Browser version
if (this. bs_name== ' IE ')
{
var _msie = Appver.match (/msie \d./i). toString ();
This. Bs_version = this. Iever = _msie.replace (/msie (\d)./I, "$");
}else
{
This. Bs_version = AppVer;
}
This. Ie6 = this. Iever==6? True:false;
This. Ie7 = this. Iever==7? True:false;
This. Ie8 = this. Iever==8? True:false;
}
Client Information
var cminfo = new Clientmentinfo ();
Cache background picture under IE6
if (CMINFO.IE6)
{
Document.execcommand ("Backgroundimagecache", false, true);
}
Set check box select or select All
function Setallcheckbox (formname,objname,num)
{
if (formName)
_arr = GetObject (formName). Elements[objname];
Else
_arr = typeof (objname) = = "Object"? ObjName:document.all (objname);
if (_arr)
{
if (num)
{
if (!_arr.length)//Only one check box, length = undefined
_arr.checked = true;
Else
{
for (var i=0; i<_arr.length; i++)
{
_arr[i].checked = true;
}
}
}else
{
if (!_arr.length)//Only one check box, length = undefined
_arr.checked = false;
Else
{
for (var i=0; i<_arr.length; i++)
{
_arr[i].checked = false;
}
}
}
}
}

Causes the current page to jump to a specified number of pages
function Gopage (PAGENUM,PAGESTR)
{
Window.location.href = "np=" +PAGENUM+PAGESTR;
}
Go to string before and after, left, right space
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, "")}


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/linyuanxing/archive/2008/12/01/3421058.aspx

Note that globalpara.js is commonly used by the author of some of the common variables and constants of each station's files, if you do not need such a file, you can put
To load a public variable JS
Loadjscssfile (js_path+ "globalpara.js");
Delete these two sentences, or they will make an error.
(Updated 2009-06-04)
Change the Clientmentinfo class to compatible Ie6,ie7,ie8,vista,windows 7 and Firefox

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.