一些比较实用的javascript方法收集,留着有用

动态加载javascript文件 

<script type="text/javascript">  
//<!--  
/*动态加载方法*/  
function loadScript(url, callback){  
  var script = document.createElement("script");  
  script.type = "text/javascript";  
  if(script.readyState){  
  //IE  
    script.onreadystatechange = function (){  
      if(script.readyState == "loaded" || script.readyState == ‘complete‘){  
        script.onreadystatechange = null;  
        callback();  
      }  
    };  
  }else{  
  //其他浏览器  
    script.onload = function (){  
      callback();  
    };  
  }  
  script.src = url;  
  document.getElementsByTagName("head")[0].appendChild(script);  
};  
/* 
 * 以下代码是先加载jquery-1.5.1.min.js,等待jquery-1.5.1.min.js加载完之后 
 * 再加载file2.js,以此类推最后加载file3.js 
*/  
loadScript("js/jquery-1.5.1.min.js", function(){  
  loadScript("file2.js", function(){  
    loadScript("file3.js", function(){  
      alert("All files are loaded!");  
    });  
  });  
});  
//-->  
</script>  



function GetXmlHttpReuqest(){  
    var xmlHttp=null;  
    try  
    {  
        xmlHttp = new XMLHttpRequest();  //对于firefox chrome等浏览器  
    }  
    catch(e)  
    {  
        try  
        {  
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //对于IE浏览器  
        }  
        catch(e)  
        {  
            try  
            {  
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
            }  
            catch(e)  
            {  
                xmlHttp = false;  
            }  
        }  
    }  
      
return false;  
}  



//js判断浏览器  
<script>  
window["MzBrowser"]={};  
(function(){  
    if(MzBrowser.platform) return;  
    var ua = window.navigator.userAgent;  
        MzBrowser.platform = window.navigator.platform;  
        MzBrowser.firefox = ua.indexOf("Firefox")>0;  
        MzBrowser.opera = typeof(window.opera)=="object";  
        MzBrowser.ie = !MzBrowser.opera && ua.indexOf("MSIE")>0;  
        MzBrowser.mozilla = window.navigator.product == "Gecko";  
        MzBrowser.netscape= window.navigator.vendor=="Netscape";  
        MzBrowser.safari= ua.indexOf("Safari")>-1;  
    if(MzBrowser.firefox) var re = /Firefox(\s|\/)(\d+(\.\d+)?)/;  
    else if(MzBrowser.ie) var re = /MSIE( )(\d+(\.\d+)?)/;  
    else if(MzBrowser.opera) var re = /Opera(\s|\/)(\d+(\.\d+)?)/;  
    else if(MzBrowser.netscape) var re = /Netscape(\s|\/)(\d+(\.\d+)?)/;  
    else if(MzBrowser.safari) var re = /Version(\/)(\d+(\.\d+)?)/;  
    else if(MzBrowser.mozilla) var re = /rv(\:)(\d+(\.\d+)?)/;  
    if("undefined"!=typeof(re)&&re.test(ua))  
    MzBrowser.version = parseFloat(RegExp.$2);  
})();  
function abc()  
{  
    if(MzBrowser.ie)  
    {  
        alert(‘ie‘);  
    }  
    if(MzBrowser.firefox)  
    {  
        alert(‘firefox‘);  
    }  
    alert(MzBrowser.version);  
}  
abc();  
</script>  
</head>  
<body>  
<hr>  
  
<script type="text/javascript">  
    var browser=navigator.appName;  
    var b_version=navigator.appVersion;  
    var version=parseFloat(b_version);  
    document.write("Browser name: "+ browser);  
    document.write("<br>");  
    document.write("Browser version: "+ version);  
</script>  
<hr>  
  
<script type="text/javascript">  
    document.write("Browser: ");  
    document.write(navigator.appName + "");  
    document.write("Browserversion: ");  
    document.write(navigator.appVersion + "");  
    document.write("Code: ");  
    document.write(navigator.appCodeName + "");  
    document.write("Platform: ");  
    document.write(navigator.platform + "");  
    document.write("Cookies enabled: ");  
    document.write(navigator.cookieEnabled + "");  
    document.write("Browser‘s user agent header: ");  
    document.write(navigator.userAgent + "");  
</script>  
<hr>  
<script type="text/javascript">  
    var x = navigator  
    document.write("CodeName=" + x.appCodeName);  
    document.write("<br>");  
    document.write("MinorVersion=" + x.appMinorVersion);  
    document.write("<br>");  
    document.write("Name=" + x.appName);  
    document.write("<br>");  
    document.write("Version=" + x.appVersion);  
    document.write("<br>");  
    document.write("CookieEnabled=" + x.cookieEnabled);  
    document.write("<br>");  
    document.write("CPUClass=" + x.cpuClass);  
    document.write("<br>");  
    document.write("OnLine=" + x.onLine);  
    document.write("<br>");  
    document.write("Platform=" + x.platform);  
    document.write("<br>");  
    document.write("UA=" + x.userAgent);  
    document.write("<br>");  
    document.write("BrowserLanguage=" + x.browserLanguage);  
    document.write("<br>");  
    document.write("SystemLanguage=" + x.systemLanguage);  
    document.write("<br>");  
    document.write("UserLanguage=" + x.userLanguage);  
</script>  
<hr>  
<!---->  
<script type="text/javascript">  
    var browser=navigator.appName;  
    var b_version=navigator.appVersion;  
    var version=parseFloat(b_version);  
    if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))  
    {  
        document.write("Your browser is good enough!");  
    }else{  
        document.write("It‘s time to upgrade your browser!");  
    }  
</script>  
 

//始终将divTop置于窗口的顶部  
<script type="text/javascript">  
<!--  
window.onscroll =function(){  
  var divTop = document.getElementById("divTop");  
  divTop.style.top = document.body.scrollTop;  
}  
//-->  
</script>  
  
//html code  
<div id ="divTop" style = "position:absolute; top :0px;right:0px; color:#cccccc; width:100px;height:20px; backgrond-colot:naty;font-size:12px;">divTop</div>  
 

<script type="text/javascript">  
//<![CDATA[  
(function(w){  
    alert(‘load start turn.‘);  
})(window);  
  
//监听浏览器  
var MSG_UNLOAD="确定离开?";  
var UnloadConfirm = {};  
  
//启用监听浏览器刷新、关闭的方法  
UnloadConfirm.set = function(confirm_msg){  
    window.onbeforeunload = function(event){  
        event = event || window.event;  
        event.returnValue = confirm_msg;  
    }  
}  
  
//关闭监听浏览器刷新、关闭的方法  
UnloadConfirm.clear = function(){  
    window.onbeforeunload = function(){};  
}  
UnloadConfirm.set(MSG_UNLOAD);  
//]]-->  
</script>  
 

//只能输入数字和小数点  
            function checkkey(value,e){   
                var key = window.event?e.keyCode:e.which;   
                if( (key > 95 && key < 106) ||   
                    (key > 47 && key < 60) ||   
                    (key == 110 && value.indexOf(".") < 0 )||   
                    (key == 190 && value.indexOf(".") < 0 )){   
                }else if(key !=8){   
                    if (window.event) //IE   
                    {   
                        e.returnValue = false;   //event.returnValue=false    
                    }   
                    else //Firefox   
                    {   
                        e.preventDefault();   
                    }   
                }   
            }   
 

/* 
 * @检查复选框至少选择一项function 
 * @create 11/29/2010 
*/  
function check_check(checkname,message,msgid,ML){  
    var flag;  
    var checks = document.getElementsByName(checkname);  
    for(var i=0;i<checks.length;i++){  
        if(checks[i].checked==true){  
            flag = true;  
            return true;  
            break;  
        }  
    }  
    if(!flag){  
        //alert(message);  
        var msgid = document.getElementById(msgid);  
        var msgObj = document.createElement("div");  
        msgObj.innerHTML = message;  
        msgObj.style.marginLeft = ML;  
        msgObj.style.color = "#f00";  
        msgid.appendChild(msgObj);  
        return false;  
    }  
}   
 

/** 
 *iframe自适应高度代码 
 *  
 */  
function iFrameHeight(id){  
    var cwin=document.getElementById(id);  
    if (document.getElementById)  
    {  
        if (cwin && !window.opera){  
            if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight){  
               cwin.height = cwin.contentDocument.body.offsetHeight;  
            }  
            else if(cwin.Document && cwin.Document.body.scrollHeight){  
               cwin.height = cwin.Document.body.scrollHeight;  
            }  
        }else{  
            if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)  
            cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera  
        }   
    }  
}  
 

//Simple closure example  
var list = document.getElementById(‘list‘).getElementsByTagName(‘li‘);  
for (var i=0; i<list.length; i++)  
{  
    list[i].onclick = (function(n){  
        return function (){  
            var li = this.parentNode.getElementsByTagName(‘li‘);  
            alert(li.length);  
            alert(n);         
        }  
    })(i);  
}  



//common javascript code write copyright  
copyright=new Date();  
update=copyright.getFullYear();  
document.write("© " + update + " Artist Rise, All Rights Reserved.");  




//JS 控制 随滚动条移动的层  
<script language="javascript">  
var SP = {};  
  
SP.getId = function(id){  
  return document.getElementById(id);  
};  
  
SP.sCenter = function(ele) {  
  var docEle = document.documentElement,  
      docBody = document.body,  
      Ele = SP.getId(ele),  
      Top = docEle.scrollTop+docBody.scrollTop+(docEle.clientHeight-Ele.offsetHeight)/2,  
      Left = docEle.scrollLeft+(docEle.clientWidth-Ele.offsetWidth)/2;  
      console.log(Ele.offsetHeight+‘,‘+docEle.clientHeight);  
    Ele.style.top = Top+"px";  
    Ele.style.left = Left+"px";  
};  
SP.sLeftTop = function(ele){  
  var docEle = document.documentElement,  
      docBody = document.body,  
      Ele = SP.getId(ele),  
      Top = docEle.scrollTop+docBody.scrollTop,  
      Left = docEle.scrollLeft;  
    Ele.style.top = Top+"px";  
    Ele.style.left = Left+"px";    
};  
SP.sLeftBottom = function(ele) {  
  var docEle = document.documentElement,  
      docBody = document.body,  
      Ele = SP.getId(ele),  
      Top = docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight,  
      Left = docEle.scrollLeft;  
    Ele.style.top = Top+"px";  
    Ele.style.left = Left+"px";  
};  
SP.sRightTop = function(ele){  
  var docEle = document.documentElement,  
      docBody = document.body,  
      Ele = SP.getId(ele),  
      Top = docEle.scrollTop+docBody.scrollTop,  
      Left = docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth;  
    Ele.style.top = Top+"px";  
    Ele.style.left = Left+"px";    
};  
SP.sRightBottom = function(ele) {  
  var docEle = document.documentElement,  
      docBody = document.body,  
      Ele = SP.getId(ele),  
      Top = docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight,  
      Left = docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth;  
    Ele.style.top = Top+"px";  
    Ele.style.left = Left+"px";  
};  
  
//init  
function init(){  
  SP.sCenter(‘redcell‘);  
  SP.sLeftTop(‘redcell2‘);  
  SP.sLeftBottom(‘redcell3‘);  
};  
  
window.onscroll = init;  
window.onresize = init;  
window.onload = init;  
</script>  

  

一些比较实用的javascript方法收集,留着有用,布布扣,bubuko.com

一些比较实用的javascript方法收集,留着有用

上一篇:javascript 获取渲染样式


下一篇:C++学习笔记2