JavaScript 应用类库-S.Sams Lifexperience ScriptClassLi
 
 http://www.cnblogs.com/Files/S.Sams/SamsJavaScriptLibrary.rar 
  
 
  阅读代码
  编辑代码
  运行效果
  复制HTML代码
  保存代码
  
  
  
<script language="javascript">
 
function $(element) {
  
if (arguments.length > 1) {
    
for (
var i = 0, elements = [], length = arguments.length; i < length; i++)
      elements.push($(arguments[i]));
    
return elements;
  }
  
if (typeof element == 
'string')
    element = 
document.getElementById(element);
  
return element;
}
var Sams_browse = {
    
    checkBrowser : 
function ()
    { 
                
this.ver=navigator.appVersion 
                
this.dom=
document.getElementById?1:0 
                
this.ie6=(
this.ver.indexOf(
"MSIE 6")>-1 && 
this.dom)?1:0; 
                
this.ie5=(
this.ver.indexOf(
"MSIE 5")>-1 && 
this.dom)?1:0; 
                
this.ie4=(
document.all && !
this.dom)?1:0; 
                
this.ns5=(
this.dom && parseInt(
this.ver) >= 5) ?1:0; 
                
this.ns4=(
document.layers && !
this.dom)?1:0; 
                
this.mac=(
this.ver.indexOf(
'Mac') > -1) ?1:0; 
                
this.ope=(navigator.userAgent.indexOf(
'Opera')>-1); 
                
this.ie=(
this.ie6 || 
this.ie5 || 
this.ie4) 
                
this.ns=(
this.ns4 || 
this.ns5) 
                
this.bw=(
this.ie6 || 
this.ie5 || 
this.ie4 || 
this.ns5 || 
this.ns4 || 
this.mac || 
this.ope) 
                
this.nbw=(!
this.bw) 
                
return this;
    },
    
    SetDefault : 
function ()
    {
        
this.
style.behavior=
'url(#default#homepage)';
        
this.setHomePage(
this.GetUrl());
        
return false;
    },
    
    SetCopy    : 
function (Msg){
        
if(navigator.userAgent.toLowerCase().indexOf(
'ie') > -1) {
            clipboardData.setData(
'Text',Msg);
            alert (
"网址“"+Msg+
"”/n已经复制到您的剪贴板中/n您可以使用Ctrl+V快捷键粘贴到需要的地方");
        }
        
else 
        {
            prompt(
"请复制网站地址:",Msg); 
        }
    },
    
    AddBookmark : 
function (site, url){
        
if(navigator.userAgent.toLowerCase().indexOf(
'ie') > -1) {
            
window.external.addFavorite(url,site)
        } 
else if (navigator.userAgent.toLowerCase().indexOf(
'opera') > -1) {
            alert (
"请使用Ctrl+T将本页加入收藏夹");
        } 
else {
            alert (
"请使用Ctrl+D将本页加入收藏夹");
        }
    },
    
    OpenWindows : 
function (url,width,height)
    {
        
window.open(url,
'newwin',
'width='+width+
',height='+height);
        
return false;
    },
    
    CloseError : 
function(){
        
window.onerror = 
function(){
return true;};
    },
    
    GetUrl : 
function(){
        
return location.href;
    },
    
    GetUrlParam : 
function(){
        
return location.search;
    },
    
    
    GetFrom : 
function(){
        
return document.referrer;
    },
    
    Request : 
function(name){
        
var GetUrl = 
this.GetUrl();
        
var Plist = 
new Array();
        
if(GetUrl.indexOf(
'?') > 0)
        {
            Plist = GetUrl.split(
'?')[1].split(
'&');
        }
        
else if(GetUrl.indexOf(
'#') > 0)
        {
            Plist = GetUrl.split(
'#')[1].split(
'&');
        }
        
if (GetUrl.length > 0)
        {
            
for(
var i=0; i<Plist.length; i++)
            {
                
var GetValue = Plist[i].split(
'=');
                
if (GetValue[0].toUpperCase() == name.toUpperCase())
                {
                    
return GetValue[1];
                    
break;
                }
            }
            
return;
        }
    },
    
    Popmsg : 
function PopIt(title,msg)
    {
        
var popup = 
window.open(
'',
'popDialog',
'height=500,width=400,scrollbars=yes');
        popup.
document.write(
'<html><title>'+title+
'</title><style>body{margin:10px;font:13px Arial;}span{text-line:20px;}</style><body><span style=/'font:14px arial;/'>'+msg + 
'</span></body></html>');
        popup.
document.close();
    }
};
var Sams_object = {
    
    
    CreateDiv : 
function (ID,ClassName,SetValue,ToDiv){
        
var creatediv = 
document.createElement(
'div');
        
if(ID != 
null) creatediv.id = ID;
        creatediv.
style.position = 
'absolute';
        
if(ClassName != 
null) creatediv.className = ClassName;
        
if(
this.Get(ToDiv))
        {
            
this.Get(ToDiv).appendChild(creatediv);
        }
        
else
        {
            
document.getElementsByTagName(
'body')[0].appendChild(creatediv);
        }
        
this.SetValue(ID,SetValue);
        
return this.Get(ID);
    },
    
    
    DeleteDiv : 
function (objid)
    {
        
try
        {
            
if(
this.Get(objid))
            {
                
var GetParent = 
this.Get(objid).parentNode;
                GetParent.removeChild(
this.Get(objid));
                
return true;
            }
            
else
            {
                
return false;
            }
        }
        
catch(e)
        {
            
return false;
        }
    },
    
    Get : 
function (objid) {
        
if (arguments.length > 1) {
            
for (
var i = 0, objids = [], length = arguments.length; i < length; i++)
              objids.push(
this.Get(arguments[i]));
            
return objids;
        }
        
if (typeof objid == 
'string')
        {
            
if (
document.getElementById) {
                objid = 
document.getElementById(objid);
            } 
else if (
document.all) {
                objid = 
document.all[objid];
            } 
else if (
document.layers) {
                objid = 
document.layers[objid];
            }
        }
        
return objid;
    },
    
    
    GetValue : 
function (objid) {
        
if (typeof objid == 
'string')
        {
            
var getTagName = 
this.Get(objid).tagName.toLowerCase();
            
if (getTagName == 
'input' || getTagName == 
'textarea' || getTagName == 
'select')
            {
                
return this.Get(objid).value;
            }
            
else if (getTagName == 
'div' || getTagName == 
'span')
            {
                
return this.Get(objid).innerText;
            }
        }
        
else if (typeof objid == 
'object')
        {
            
return objid.value;
        }
    },
    
    SetValue: 
function(objid,inserValue) {
        
var getTagName = 
this.Get(objid).tagName.toLowerCase();
        
if (inserValue == 
null) inserValue = 
'';
        
if (getTagName == 
'input' || getTagName == 
'textarea')
        {
            
this.Get(objid).value = inserValue;
        }
        
else if (getTagName == 
'div' || getTagName == 
'sapn')
        {
            
            
this.Get(objid).innerText = inserValue;
        }
    },
    
    
    CopyCode : 
function (str) {
        
var rng = 
document.body.createTextRange();
        rng.moveToElementText(str);
        rng.scrollIntoView();
        rng.select();
        rng.execCommand(
"Copy");
        rng.collapse(
false);
    },
    
    
    ShowHidd : 
function(objid,isshow){
        
if (isshow != 
null)
        {
            
if(isshow)
            {
                
this.Get(objid).
style.display = 
'block';
            }
            
else
            {
                
this.Get(objid).
style.display = 
'none';
            }
        }
        
else
        {
            
if(
this.Get(objid).
style.display == 
'none')
            {
                
this.Get(objid).
style.display = 
'block';
            }
            
else
            {
                
this.Get(objid).
style.display = 
'none';
            }
        }
    },
    
    
    IsVisible : 
function(objid){
        
if(
this.Get(objid))
        {
            
try
            {
                
if (
this.Get(objid).
style.display == 
'none')
                {
                    
return false
                }
                
if(
this.Get(objid).
style.visibility == 
'hidden')
                {
                    
return false;
                }
                
return true;
            }
            
catch(e)
            {
                
return false;
            }
        }
        
else
        {
            
return false;
        }
    }
};
var Sams_string = {
    
    Left : 
function (str,n)
    {
        
if(str.length > 0)
        {
            
if(n>str.length) n = str.length;
            
return str.substr(0,n)
        }
        
else
        {
         
return;
        }
    },
    
    
    Right : 
function (str,n)
    {
        
if(str.length > 0)
        {
            
if(n>=str.length) 
return str;
            
return str.substr(str.length-n,n);
        }
        
else
        {
            
return;
        }
    },
    
    
    Trim : 
function (str) 
    {
        
if (typeof str == 
'string') 
return str.replace(/(^/s*)|(/s*$)/g, 
'');
    },
    
    Ltrim : 
function (str) 
    { 
        
if (typeof str == 
'string') 
return str.replace(/(^/s*)/g, 
'');
    },
    
    Rtrim : 
function (str) 
    { 
        
if (typeof str == 
'string') 
return str.replace(/(/s*$)/g, 
'');
    },
    
    
    strip : 
function(str) {
        
if (typeof str == 
'string') 
return str.replace(/^/s+/, 
'').replace(/(^/s*)|(/s*$)/g, 
'');
    },
    
    stripTags : 
function(str) {
        
if (typeof str == 
'string')
return str.replace(/<//?[^>]+>/gi, 
'').replace(/(^/s*)|(/s*$)/g, 
'');
    }
};
var Sams_time = {
    
    GetDateNow : 
function (){
        
var d,y,m,dd;
        d = 
new Date(); 
        y = d.getYear();
        m = d.getMonth() + 1;
        dd = d.getDate();                                                
        
return y+
"-"+m+
"-"+dd;
    },
     
    AddDays : 
function(toDate,N){
        
var aDate=
this._cvtISOToDate(toDate);
        
if (!aDate) 
return "";
        
var millis=86400000 * N;
        aDate=
new Date(aDate.getTime()+millis);
        
return this._fmtDateISO(aDate);
    },
    _fmtDateISO : 
function (aDate) {
        
with (aDate) {
            
var mm=getMonth()+1;
            
if (mm<10) {mm=
'0'+mm;}
            
var dd=getDate();
            
if (dd<10) {dd=
'0'+dd;}
            
return (getFullYear() + 
'-' + mm + 
'-' + dd);
        }
    },
    _cvtISOToDate : 
function (isoDate) {
        
var atomDate= isoDate.split(
'-');
        
var aDate=
new Date(parseInt(atomDate[0],10),parseInt(atomDate[1],10)-1,parseInt(atomDate[2],10),6,0,0);
        
return aDate;
    }
};
var Sams_media = {
    
    ZoomFun : 
function(objid){
        Sams_object.Get(objid).onmousewheel = 
function(){
return Sams_media.imagecontrol(
this);}
    },
     
    ResizeImage: 
function (IntSize) {
        
var imgsinlog=
document.getElementsByTagName(
'img');
        
for(j=0; j<imgsinlog.length; j++) {
            
if (imgsinlog[j].width >= IntSize) {
                imgsinlog[j].width = IntSize;
                imgsinlog[j].
style.cursor= 
'pointer';
                imgsinlog[j].onclick = 
function() {
window.open(
this.src);}
                
if (navigator.userAgent.toLowerCase().indexOf(
'ie') > -1) {
                    imgsinlog[j].title = 
'您可以用鼠标中键或者使用Ctrl+鼠标滚轮缩放图片,点击图片可在新窗口打开';
                    imgsinlog[j].onmousewheel = 
function(){
return Sams_media.imagecontrol(
this);};
                }
                
else
                {
                    imgsinlog[j].title = 
'点击图片可在新窗口打开';
                }
            }
        }
    },
    imagecontrol : 
function(obj){
        
var zoom=parseInt(obj.
style.zoom, 10)||100;zoom+=
event.wheelDelta/12;
        
if (zoom>0) obj.
style.zoom=zoom+
'%';
        
return false;
    },
     
    ImagesError : 
function(errimgpath){
        
var imglist = 
document.getElementsByTagName(
'img');
        
for(j=0; j<imglist.length; j++) {
            imglist[j].onerror = 
function(){
                
this.src = errimgpath;
            }
        }
    },
    
    ShowMedia : 
function (mFile, mFileType, ObjID, mWidth, mHeight) {
        
var mediaStr;
        
switch(mFileType){
            
case "swf":
                mediaStr=
"<object codeBase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+mWidth+
"' height='"+mHeight+
"'><param name='movie' value='"+mFile+
"'><param name='quality' value='high'><param name='AllowScriptAccess' value='never'><embed src='"+mFile+
"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+mWidth+
"' height='"+mHeight+
"'></embed></OBJECT>";
                
break;
            
default :
                mediaStr=
"<object width='"+mWidth+
"' height='"+mHeight+
"' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><param name='url' value='"+mFile+
"' /><embed width='"+mWidth+
"' height='"+mHeight+
"' type='application/x-mplayer2' src='"+mFile+
"'></embed></object>";
        }
        
        
var mediaDiv = Sams_object.Get(ObjID);
        
        
if (mediaDiv) {
            mediaDiv.innerHTML = mediaStr;
        } 
        
else
        {
            mediaDiv = 
document.createElement(
"div");
            mediaDiv.id = ObjID;
            mediaDiv.innerHTML = mediaStr;
            
document.getElementsByTagName(
'body')[0].appendChild(mediaDiv);
        }
        
return false;
    }
};
var Sams_style = {
    
    doZoom : 
function (objid,size){
        Sams_object.Get(objid).
style.fontSize=size+
'px';
    },
    
    ClassName: 
function(objid, className) {
        Sams_object.Get(objid).className = className;
    },
    
    
    GotoXY : 
function (obj) {
        
var t=obj.offsetTop; 
        
var l=obj.offsetLeft; 
        
while(obj=obj.offsetParent){ 
            t+=obj.offsetTop; 
            l+=obj.offsetLeft; 
        }
        
return Array(t,l);
    }
};
var Sams_account = {
    
    GetTen: 
function (i)
    {
        
var items_One,Get_One;
        
if (i.length > 1&& (/^/d+$/.test(i)))
        {
           items_One = i.substr(0,i.length-1);
           Get_One = i.substr(i.length-1,1);
           
if (parseInt(Get_One)>0)
           {
            items_One = parseInt(items_One)+1;
            items_One = items_One + 
'0';
           }
           
else
            {
            items_One = items_One + 
'0';    
            }
        }
        
else
        {
            items_One = i;
        }
        
return items_One;
    }
};
var Sams_validate = {
    
    IsNumber : 
function(str){
        
if (/^/d+$/.test(str)){
return true;}
else{
return false;}
    },
    
    IsNumberObj : 
function(objid){
        
return this.IsNumber(Sams_object.GetValue(objid));
    },
    
    IsInt : 
function(str){
        
if (/^(/+|-)?/d+$/.test(str)){
return true;}
else{
return false;}
    },
    
    IsIntObj : 
function(objid){
        
return this.IsInt(Sams_object.GetValue(objid));
    },
    
    
    IsChinese : 
function(str)
    {
        
if (/^[/u4e00-/u9fa5]+$/.test(str)){
return true;}
else{
return false;}
    },
    
    IsChineseObj : 
function(objid)
    {
        
return this.IsChinese(Sams_object.GetValue(objid));
    },
    
    IsLower : 
function(str)
    {
         
if (/^[A-Za-z]+$/.test(str)){
return true}
else{
return false;}
    },
    
    IsLowerObj : 
function(objid)
    {
         
return this.IsLower(Sams_object.GetValue(objid));
    },
    
    IsUrl : 
function(str)
    {
        
var myReg = /^((http:[/][/])?/w+([.]/w+|[/]/w*)*)?$/;    
        
if(myReg.test(str)){
return true;}
else{
return false;}
    },
    
    IsUrlObj : 
function(objid)
    {
        
return this.IsUrl(Sams_object.GetValue(objid));
    },
    
    IsEmail : 
function(str)
    {
        
var myReg = /^([-_A-Za-z0-9/.]+)@([_A-Za-z0-9]+/.)+[A-Za-z0-9]{2,3}$/;    
        
if(myReg.test(str)){
return true;}
else{
return false;}
    },
    
    
    
    IsEmailObj : 
function(objid)
    {
        
return this.IsEmail(Sams_object.GetValue(objid));
    },
    
    IsMobile : 
function(str)
    {
        
var regu =/(^[1][3][0-9]{9}$)|(^0[1][3][0-9]{9}$)/;   
        
var re = 
new RegExp(regu);   
        
if (re.test(str)){
return true;}
else{
return false;}
    },
    
    IsMobileObj : 
function(objid)
    {
        
return this.IsMobile(Sams_object.GetValue(objid));
    }
};
var Sams_ajax = {
    _objPool: [],
    _getInstance: 
function (){
        
for (
var i = 0; i < 
this._objPool.length; i ++){
            
if (
this._objPool[i].readyState == 0 || 
this._objPool[i].readyState == 4){
                
return this._objPool[i];
            }
        }
        
this._objPool[
this._objPool.length] = 
this._createObj();
        
return this._objPool[
this._objPool.length - 1];
    },
     _createObj : 
function (){
        
if (
window.XMLHttpRequest){
            
var objXMLHttp = 
new XMLHttpRequest();
        }
        
else{
            
var MSXML = [
'MSXML2.XMLHTTP.5.0', 
'MSXML2.XMLHTTP.4.0', 
'MSXML2.XMLHTTP.3.0', 
'MSXML2.XMLHTTP', 
'Microsoft.XMLHTTP'];
            
for(
var n = 0; n < MSXML.length; n ++){
                
try{
                    
var objXMLHttp = 
new ActiveXObject(MSXML[n]);
                    
break;
                }
                
catch(e){
                }
            }
         }          
        
if (objXMLHttp.readyState == 
null){
            objXMLHttp.readyState = 0;
            objXMLHttp.addEventListener(
"load", 
function (){
                    objXMLHttp.readyState = 4;
                    
if (typeof objXMLHttp.onreadystatechange == 
"function"){
                        objXMLHttp.onreadystatechange();
                    }
                },  
false);
        }
        
return objXMLHttp;
    },
        SendRequest : 
function (method, url, data, callback,funparam,funparam2){
        
var objXMLHttp = 
this._getInstance();
        
with(objXMLHttp){
            
try{
                
if (url.indexOf(
"?") > 0){
                    url += 
"&randnum=" + Math.random();
                }
                
else{
                    url += 
"?randnum=" + Math.random();
                }
                open(method, url, 
true);            
                setRequestHeader(
'Content-Type', 
'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = 
function (){
                    
if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                    {
                        callback(objXMLHttp,funparam,funparam2);
                    }
else{
                        callback(
null,funparam,funparam2);
                    }
                }
            }
            
catch(e){
                alert(e);
            }
        }
    }
};
var Sams_cookies = {
    
    setCookie : 
function (name, value)
    {
        
try
        {
            
var argv = setCookie.arguments;
            
var argc = setCookie.arguments.length;
            
var expires = (argc > 2) ? argv[2] : 
null;
            
if(expires!=
null)
            {
                
var LargeExpDate = 
new Date ();
                LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
            }
            
document.cookie = name + 
"=" + escape (value)+((expires == 
null) ? 
"" : (
"; expires=" +LargeExpDate.toGMTString()));
            
return true;
        }
        
catch(e)
        {
            
return false;
        }
    },
    
    getCookie : 
function (Name)
    {
        
var search = Name + 
"="
        if(
document.cookie.length > 0) 
        {
            offset = 
document.cookie.indexOf(search)
            
if(offset != -1) 
            {
                offset += search.length
                end = 
document.cookie.indexOf(
";", offset)
                
if(end == -1) end = 
document.cookie.length
                
return unescape(
document.cookie.substring(offset, end))
            }
            
else
            {
                
return;
            }
        }
    }
};
</script>
<img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" id="I"><br/>
<input type="button" value="指定日期加N天后的日期值" οnclick="alert(Sams_time.AddDays('2006-12-12','123'));">
<input type="button" value="请求URL指定参数的值" οnclick="alert(Sams_browse.Request('o'));">
<input type="button" value="显示隐藏Medio" οnclick="Sams_object.ShowHidd('I');">
<input type="button" value="字符处理" οnclick="Sams_object.SetValue('ToHtml',Sams_string.stripTags(Sams_object.GetValue('Content')));">
<input type="button" value="清除字符" οnclick="Sams_object.SetValue('ToHtml');Sams_object.SetValue('S','S.Sams lifexperience!');">
<input type="button" value="FormValue" οnclick="var ads = Sams_object.Get('Content','ToHtml');for(var s in ads){alert(ads[s].value);};">
<input type="button" value="CopyUrl" οnclick="Sams_browse.SetCopy(Sams_browse.GetUrl());"> 
<div id="medias"></div>
<textarea name="Content" wrap="virtual" id="Content" style="width:100%;height:100px;background:#F9F9F9;">    <STRONG>公交车
</STRONG><BR>  北京的公交车为人们的出行提供了很大方便,不仅有普通的公共电车或汽车,还有“8”字头的空调车、“特”字头的特线双层巴士及夜班车(201路到212路)等,票价有多种,其中空调车、特线车、夜班车无论是否开空调,票价一律为2元/人,有人售票车则按线路长短实行多级制票价。
</P>
</textarea>
<div style="display:;">
<textarea id="ToHtml" style="width:100%;height:100px;background:#FFFFCC;"></textarea>
</div>
<div id="S">夜班车无论是否开空调,票价一律为2元/人,有人售票车则按线路长短实行多级制票价
</div>
<style>
.AlertDivCity_Class
{background:#eeeeee;
width:150px;
height:auto;
position:absolute;
}
</style>
<script language="JavaScript">
<!--
    Sams_media.ZoomFun(
'I');
        
            
if(
document.getElementById(
'ToHtml').parentNode.
style.display != 
'none')Sams_object.Get(
'ToHtml').focus();
</script>