Common JS Function Code

Source: Internet
Author: User

CopyCode The Code is as follows: // get the object
Function GetObject (objectid, top)
{
Doc = Top? Optional topics top.doc ument: document;
If (typeof (objectid )! = "Object" & typeof (objectid )! = "Function ")
{
If (Doc. getelementbyid & Doc. getelementbyid (objectid ))
{
// W3C 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;
}
// Obtain the relative path
Function getrelativepath ()
{
VaR url = location. href; // The current URL
VaR urlcs = string (location. Search );
Url = URL. Replace (urlcs ,"");
VaR Path = URL. length-URL. replace (// G ,""). length-3; // The level is the length of the URL containing/-no length of the contained/, and then the number of project headers /.

VaR STR = "";
For (VAR I = 0; I <path; I ++)
{
STR + = "../"; // returns the result of combining the string with a relative path.
}
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 +" "/> ");
}
}
}
// Define 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 /';

// Js for loading public variables
Loadjscssfile (js_path + "globalpara. js ");

// Set an item to be selected in the drop-down list
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 based on the option text in the drop-down list
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 the single-choice 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;
}
}
}

// Select certain items in the check box based on ID
// Vlu format: 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;
}
}
}

// Quick input, fromobj: source object, toobjid: ID of the target object. If txt is true, the innerhtml value of toobjid is obtained.
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 an image before uploading it
You can also check the image size in IE6.
Size in 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', 400)"/>
<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 selected not an image file. Please reselect .");
}
}
Function previewimgnow (imgdiv, imgfile)
{
Try {GetObject (imgdiv). Filters. Item ("DXImageTransform. Microsoft. alphaimageloader"). src = imgfile} catch (e ){}
}
// Check the image size before uploading
// The condition is that the function is triggered when the file is changed to display the thumbnail on the IMG.
// The unit of size is kb.
Function checkimgsize (IMG, size)
{
IMG = GetObject (IMG );
If (IMG. readystate = "complete ")
{
VaR Limit = size * 1024;
If (IMG. filesize> limit)
{
Alert ("error! The size of the image you uploaded is "+ (parseint (IMG. filesize/1024) +" kb, which exceeds the "+ size +" kb limit. please upload it again ");
Return false
} Else
Return true;
}
Return true;
}
// Check 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)
{< br> return 'windows Vista ';
}else if (appver. indexof ("Windows NT 6.1")>-1 | appver. indexof ("Windows 7")>-1)
{< br> return 'windows 7 ';
}else
{< br> try {
VaR _ winname = array ('123', 'xp ', '123 ');
VaR _ ntnum = appver. match (/Windows NT 5. \ D/I ). tostring ();
return 'windows' + _ winname [_ ntnum. replace (/Windows NT 5. (\ D)/I, "$1")];
}catch (e) {return 'windows ';}
}< BR >}else if (_ pF = "mac68k" | _ pF = "macppc" | _ pF = "Macintosh ")
{< br> return "Mac";
}else if (_ pF = "X11")
{< br> return "Unix ";
} else if (string (_ pF ). indexof ("Linux")>-1)
{< br> return "Linux";
}else
{< br> return "unknow ";
}< BR >}

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, "$1 ");
} 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 the background image in IE6
If (cminfo. IE6)
{
Document.exe ccommand ("backgroundimagecache", false, true );
}
// Set all or none of the check boxes
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) // if there is 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) // if there is only one check box, length = undefined
_ Arr. Checked = false;
Else
{
For (VAR I = 0; I <_ arr. length; I ++)
{
_ Arr [I]. Checked = false;
}
}
}
}
}

// Jump the current page to the specified page number
Function gopage (pagenum, pagestr)
{
Window. Location. href = "? NP = "+ pagenum + pagestr;
}
// Remove spaces on the left and right of the string respectively
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 the csdn blog, reproduced please indicate the source: http://blog.csdn.net/linyuanxing/archive/2008/12/01/3421058.aspx

Note that the authors of globalpara. js often use some files of common variables and constants for each station. If you do not need such files
// Js for loading public variables
Loadjscssfile (js_path + "globalpara. js ");
If you delete these two sentences, an error will be reported.
(Updated on)
Changed the clientmentinfo class to compatible with 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.