Copy codeThe Code is as follows:
// Obtain 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)
{
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 ('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 ';}
}
} 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, "$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.