SoFunction
Updated on 2025-02-28

Some commonly used JS function functions (updated on 2009-06-04)


//Get object
function getObject(objectId,top)
{
    doc = top?:document;
    if(typeof(objectId)!="object" && typeof(objectId)!="function")
    {
        if( && (objectId))
         {
         // W3C DOM
         return (objectId);
         }
        else if((objectId))
        {
            return (objectId)[0];
        }
         else if ( && (objectId))
         {
         // MSIE 4 DOM
         return (objectId);
         }
         else if ( && [objectId])
         {
         // NN 4 DOM.. note: this won't find nested layers
         return [objectId];
         }
         else
         {
         return false;
         }
    }else
        return objectId;
}
//Get relative path
function getRelativePath()
{
var url = ;//Current url
    var urlcs = String();
    url = (urlcs,"");
var path = - (/\//g,"").length - 3;    //The level is the length of url containing / - the length of no included / is subtracted from the number of project headers /

    var str = "";
    for(var i = 0; i < path; i++)
    {
str += "../";//Return a string that combines it into 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 = ("script");
            for(var i=0;i<;i++)
            {
                try{
                    if(allScripts[i].(filename)>-1)
                    {
                        had = true;
                        break;
                    }
                }catch(e){}
            }    
        }
        if(!had)
        {
            ("<script src=""+filename+"" src=""+filename+"" type='text/javascript'></script>");
        }
    }else
    {
        if(chkonce)
        {
            var allCss = ("link");
            if()
            {
                for(var i=0;i<;i++)
                {
                    try{
                        if(allCss[i].(filename)>-1)
                        {
                            had = true;
                            break;
                        }
                    }catch(e){}
                }
            }
        }
        if(!had)
        {
            ("<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/';

//Load the JS of the public variable
loadjscssfile(JS_PATH+"");

//Set an item in the drop-down table to be selected
function setSelOption(objId,vlu)
{
    objId = getObject(objId);
    for(var i=0;i<;i++)
    {
        if([i].value==vlu)
        {
            [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<;i++)
    {
        if([i].innerHTML==txt)
        {
            [i].setAttribute("selected","selected");
            break;
        }
    }    
}

//Set an item in the radio button group to be selected
function setSelRadio(objName,vlu)
{
    objName = (objName);
    for(var i=0;i<;i++)
    {
        if(objName[i].value==vlu)
        {
            objName[i].setAttribute("checked","checked");
            break;
        }
    }
}

//Set some items in the check box according to ID are selected
//vlu's format is: 1,2,3
function setSelCheckbox(prefix,vlu)
{
    var _arr = (",");
    if(_arr!="")
    {
        for(var i=0; i<_arr.length; i++)
        {
            getObject(prefix+_arr[i]).checked = true;
        }
    }
}

//Quick input, fromObj: source object, toObjId: target object ID, when txt is true, take the innerHTML value of toObjId
function fastInput(fromObj,toObjId,txt)
{
    if(=='' || ==0) return false;
    txt = !txt ? false : txt;
    var toObj = getObject(toObjId);
    if(txt)
    {
        if(typeof () == 'undefined')
              = toTxt([].innerHTML);
        else
             = toTxt([].innerHTML);
    }else
    {
        if(typeof () == 'undefined')
              = ;
        else
             = ;        
    }
}

/*
Preview pictures before uploading pictures in IE6, IE7
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:(sizingMethod=scale)
}
.nodis{display:none;}
--></style><style type="text/css" bogus="1">
.newPreview
{
    width:400px; height:300px;
    FILTER: progid:(sizingMethod=scale)
}
.nodis{display:none;}</style>
<input type="file" name="litpic" onchange="PreviewImg(this,'newPreview','ndsPreview',400)" />
<div class="newPreview"></div>
<img class="nodis" />
*/
function PreviewImg(imgFile,newPreview,ndsPreview,size)
{
    newPreview = getObject(newPreview);
    if(!imgFile || ! || !newPreview){return};
    var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$|\.bmp$/i;
    if(())
    {
        try{("").src = ;}catch(e){}
        if(("MSIE 6.0",0)>-1)
        {
            size=!size?0:size;
            ndsPreview = getObject(ndsPreview);
            if(ndsPreview)
            {
                try{("onreadystatechange", function(){checkImgSize(ndsPreview,size)})}catch(e){}
                 = ;
            }
        }
    }
    else
    {
alert("You selected not an image file, please reselect.");
    }
}
function PreviewImgNow(imgDiv,imgFile)
{
    try{getObject(imgDiv).("").src = imgFile}catch(e){}    
}
//Check the size of the image before uploading
//The condition is that the function should be triggered when the file changes to display the thumbnail on img
//size unit is KB
function checkImgSize(img,size)
{
    img = getObject(img);
    if( == "complete")
    {
        var limit = size * 1024;
        if( > limit)
        {
alert("Error! The image you uploaded is "+(parseInt(/1024))+"KB, which exceeds the limit of "+size+"KB, please upload again");
            return false
        }else
            return true;
    }
    return true;
}
//Detection of client environment
function ClientMentInfo()
{
    var me = this;    
    var appVer = ;

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

     = function (){
        var _pf = ;
        if(_pf == "Win32" || _pf == "Windows")
        {

            if(("Windows NT 6.0") > -1 || ("Windows Vista") > -1)
            {
                return 'Windows Vista';    
            }else if(("Windows NT 6.1") > -1 || ("Windows 7") > -1)
            {
                return 'Windows 7';    
            }else
            {
                try{
                    var _winName = Array('2000','XP','2003');
                    var _ntNum = (/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";    
        }        
    }

= ();       //Operating system type
     = null;
this.Bs_lang = ( == 'Netscape'?:);//Browser language version
this.Bs_Name = ();        //Browser name

//Browser version
    if(this.Bs_Name=='IE')
    {
        var _msie = (/MSIE \d./i).toString();
        this.Bs_Version = = _msie.replace(/MSIE (\d)./i,"$1");
    }else
    {
        this.Bs_Version = appVer;
    }
    this.Ie6 = ==6 ? true: false;
    this.Ie7 = ==7 ? true: false;
    this.Ie8 = ==8 ? true: false;
}
//Client information
var CMInfo = new ClientMentInfo();
//IE6 cache background image
if(CMInfo.Ie6)
{
    ("BackgroundImageCache", false, true);
}
//Set the check box to select all or not
function setAllCheckbox(formName,objName,num)
{
    if(formName)
        _arr = getObject(formName).elements[objName];
    else
        _arr = typeof(objName)=="object"?objName:(objName);
    if(_arr)
    {
        if(num)
        {
if (!_arr.length ) // There is only one check box, then length = undefined
                _arr.checked = true;
            else
            {
                for(var i=0; i<_arr.length; i++)
                {
                    _arr[i].checked = true;
                }
            }
        }else
        {
if (!_arr.length ) // There is only one check box, then length = undefined
                _arr.checked = false;
            else
            {
                for(var i=0; i<_arr.length; i++)
                {
                    _arr[i].checked = false;
                }
            }
        }
    }
}

//Make the current page jump to the specified page number page
function goPage(pageNum,pageStr)
{
     = "?np="+pageNum+pageStr;
}
//Go to the front and back of the string, left and right spaces respectively
= function(){ return (/^\s+|\s+$/g,"")}
= function(){ return (/^\s+/g,"")}
= function(){ return (/\s+$/g,"")}