zoukankan      html  css  js  c++  java
  • 一些比较实用的javascript方法收集,留着有用

    动态加载javascript文件

    Javascript代码  收藏代码
    1. <script type="text/javascript"> 
    2. //<!-- 
    3. /*动态加载方法*/ 
    4. function loadScript(url, callback){ 
    5.   var script = document.createElement("script"); 
    6.   script.type = "text/javascript"; 
    7.   if(script.readyState){ 
    8.   //IE 
    9.     script.onreadystatechange = function (){ 
    10.       if(script.readyState == "loaded" || script.readyState == 'complete'){ 
    11.         script.onreadystatechange = null; 
    12.         callback(); 
    13.       } 
    14.     }; 
    15.   }else{ 
    16.   //其他浏览器 
    17.     script.onload = function (){ 
    18.       callback(); 
    19.     }; 
    20.   } 
    21.   script.src = url; 
    22.   document.getElementsByTagName("head")[0].appendChild(script); 
    23. }; 
    24. /*
    25. * 以下代码是先加载jquery-1.5.1.min.js,等待jquery-1.5.1.min.js加载完之后
    26. * 再加载file2.js,以此类推最后加载file3.js
    27. */ 
    28. loadScript("js/jquery-1.5.1.min.js", function(){ 
    29.   loadScript("file2.js", function(){ 
    30.     loadScript("file3.js", function(){ 
    31.       alert("All files are loaded!"); 
    32.     }); 
    33.   }); 
    34. }); 
    35. //--> 
    36. </script> 
    Java代码  收藏代码
    1. function GetXmlHttpReuqest(){ 
    2.     var xmlHttp=null; 
    3.     try 
    4.     { 
    5.         xmlHttp = new XMLHttpRequest();  //对于firefox chrome等浏览器 
    6.     } 
    7.     catch(e) 
    8.     { 
    9.         try 
    10.         { 
    11.             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //对于IE浏览器 
    12.         } 
    13.         catch(e) 
    14.         { 
    15.             try 
    16.             { 
    17.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    18.             } 
    19.             catch(e) 
    20.             { 
    21.                 xmlHttp = false; 
    22.             } 
    23.         } 
    24.     } 
    25.      
    26. return false; 
    Javascript代码  收藏代码
    1. //js判断浏览器 
    2. <script> 
    3. window["MzBrowser"]={}; 
    4. (function(){ 
    5.     if(MzBrowser.platform) return; 
    6.     var ua = window.navigator.userAgent; 
    7.         MzBrowser.platform = window.navigator.platform; 
    8.         MzBrowser.firefox = ua.indexOf("Firefox")>0; 
    9.         MzBrowser.opera = typeof(window.opera)=="object"; 
    10.         MzBrowser.ie = !MzBrowser.opera && ua.indexOf("MSIE")>0; 
    11.         MzBrowser.mozilla = window.navigator.product == "Gecko"; 
    12.         MzBrowser.netscape= window.navigator.vendor=="Netscape"; 
    13.         MzBrowser.safari= ua.indexOf("Safari")>-1; 
    14.     if(MzBrowser.firefox) var re = /Firefox(s|/)(d+(.d+)?)/; 
    15.     else if(MzBrowser.ie) var re = /MSIE( )(d+(.d+)?)/; 
    16.     else if(MzBrowser.opera) var re = /Opera(s|/)(d+(.d+)?)/; 
    17.     else if(MzBrowser.netscape) var re = /Netscape(s|/)(d+(.d+)?)/; 
    18.     else if(MzBrowser.safari) var re = /Version(/)(d+(.d+)?)/; 
    19.     else if(MzBrowser.mozilla) var re = /rv(:)(d+(.d+)?)/; 
    20.     if("undefined"!=typeof(re)&&re.test(ua)) 
    21.     MzBrowser.version = parseFloat(RegExp.$2); 
    22. })(); 
    23. function abc() 
    24.     if(MzBrowser.ie) 
    25.     { 
    26.         alert('ie'); 
    27.     } 
    28.     if(MzBrowser.firefox) 
    29.     { 
    30.         alert('firefox'); 
    31.     } 
    32.     alert(MzBrowser.version); 
    33. abc(); 
    34. </script> 
    35. </head> 
    36. <body> 
    37. <hr> 
    38.  
    39. <script type="text/javascript"> 
    40.     var browser=navigator.appName; 
    41.     var b_version=navigator.appVersion; 
    42.     var version=parseFloat(b_version); 
    43.     document.write("Browser name: "+ browser); 
    44.     document.write("<br>"); 
    45.     document.write("Browser version: "+ version); 
    46. </script> 
    47. <hr> 
    48.  
    49. <script type="text/javascript"> 
    50.     document.write("Browser: "); 
    51.     document.write(navigator.appName + ""); 
    52.     document.write("Browserversion: "); 
    53.     document.write(navigator.appVersion + ""); 
    54.     document.write("Code: "); 
    55.     document.write(navigator.appCodeName + ""); 
    56.     document.write("Platform: "); 
    57.     document.write(navigator.platform + ""); 
    58.     document.write("Cookies enabled: "); 
    59.     document.write(navigator.cookieEnabled + ""); 
    60.     document.write("Browser's user agent header: "); 
    61.     document.write(navigator.userAgent + ""); 
    62. </script> 
    63. <hr> 
    64. <script type="text/javascript"> 
    65.     var x = navigator 
    66.     document.write("CodeName=" + x.appCodeName); 
    67.     document.write("<br>"); 
    68.     document.write("MinorVersion=" + x.appMinorVersion); 
    69.     document.write("<br>"); 
    70.     document.write("Name=" + x.appName); 
    71.     document.write("<br>"); 
    72.     document.write("Version=" + x.appVersion); 
    73.     document.write("<br>"); 
    74.     document.write("CookieEnabled=" + x.cookieEnabled); 
    75.     document.write("<br>"); 
    76.     document.write("CPUClass=" + x.cpuClass); 
    77.     document.write("<br>"); 
    78.     document.write("OnLine=" + x.onLine); 
    79.     document.write("<br>"); 
    80.     document.write("Platform=" + x.platform); 
    81.     document.write("<br>"); 
    82.     document.write("UA=" + x.userAgent); 
    83.     document.write("<br>"); 
    84.     document.write("BrowserLanguage=" + x.browserLanguage); 
    85.     document.write("<br>"); 
    86.     document.write("SystemLanguage=" + x.systemLanguage); 
    87.     document.write("<br>"); 
    88.     document.write("UserLanguage=" + x.userLanguage); 
    89. </script> 
    90. <hr> 
    91. <!----> 
    92. <script type="text/javascript"> 
    93.     var browser=navigator.appName; 
    94.     var b_version=navigator.appVersion; 
    95.     var version=parseFloat(b_version); 
    96.     if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4)) 
    97.     { 
    98.         document.write("Your browser is good enough!"); 
    99.     }else{ 
    100.         document.write("It's time to upgrade your browser!"); 
    101.     } 
    102. </script> 
    Code代码  收藏代码
    1. //始终将divTop置于窗口的顶部 
    2. <script type="text/javascript"> 
    3. <!-- 
    4. window.onscroll =function(){ 
    5.   var divTop = document.getElementById("divTop"); 
    6.   divTop.style.top = document.body.scrollTop; 
    7. //--> 
    8. </script> 
    9.  
    10. //html code 
    11. <div id ="divTop" style = "position:absolute; top :0px;right:0px; color:#cccccc; 100px;height:20px; backgrond-colot:naty;font-size:12px;">divTop</div> 
    Javascript代码  收藏代码
    1. <script type="text/javascript"> 
    2. //<![CDATA[ 
    3. (function(w){ 
    4.     alert('load start turn.'); 
    5. })(window); 
    6.  
    7. //监听浏览器 
    8. var MSG_UNLOAD="确定离开?"; 
    9. var UnloadConfirm = {}; 
    10.  
    11. //启用监听浏览器刷新、关闭的方法 
    12. UnloadConfirm.set = function(confirm_msg){ 
    13.     window.onbeforeunload = function(event){ 
    14.         event = event || window.event; 
    15.         event.returnValue = confirm_msg; 
    16.     } 
    17.  
    18. //关闭监听浏览器刷新、关闭的方法 
    19. UnloadConfirm.clear = function(){ 
    20.     window.onbeforeunload = function(){}; 
    21. UnloadConfirm.set(MSG_UNLOAD); 
    22. //]]--> 
    23. </script> 
    Javascript代码  收藏代码
    1. //只能输入数字和小数点 
    2.             function checkkey(value,e){  
    3.                 var key = window.event?e.keyCode:e.which;  
    4.                 if( (key > 95 && key < 106) ||  
    5.                     (key > 47 && key < 60) ||  
    6.                     (key == 110 && value.indexOf(".") < 0 )||  
    7.                     (key == 190 && value.indexOf(".") < 0 )){  
    8.                 }else if(key !=8){  
    9.                     if (window.event) //IE  
    10.                     {  
    11.                         e.returnValue = false;   //event.returnValue=false   
    12.                     }  
    13.                     else //Firefox  
    14.                     {  
    15.                         e.preventDefault();  
    16.                     }  
    17.                 }  
    18.             }  
    Javascript代码  收藏代码
    1. /*
    2. * @检查复选框至少选择一项function
    3. * @create 11/29/2010
    4. */ 
    5. function check_check(checkname,message,msgid,ML){ 
    6.     var flag; 
    7.     var checks = document.getElementsByName(checkname); 
    8.     for(var i=0;i<checks.length;i++){ 
    9.         if(checks[i].checked==true){ 
    10.             flag = true; 
    11.             return true; 
    12.             break; 
    13.         } 
    14.     } 
    15.     if(!flag){ 
    16.         //alert(message); 
    17.         var msgid = document.getElementById(msgid); 
    18.         var msgObj = document.createElement("div"); 
    19.         msgObj.innerHTML = message; 
    20.         msgObj.style.marginLeft = ML; 
    21.         msgObj.style.color = "#f00"; 
    22.         msgid.appendChild(msgObj); 
    23.         return false; 
    24.     } 
    25. }  
    Javascript代码  收藏代码
    1. /**
    2. *iframe自适应高度代码
    3. */ 
    4. function iFrameHeight(id){ 
    5.     var cwin=document.getElementById(id); 
    6.     if (document.getElementById) 
    7.     { 
    8.         if (cwin && !window.opera){ 
    9.             if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight){ 
    10.                cwin.height = cwin.contentDocument.body.offsetHeight; 
    11.             } 
    12.             else if(cwin.Document && cwin.Document.body.scrollHeight){ 
    13.                cwin.height = cwin.Document.body.scrollHeight; 
    14.             } 
    15.         }else{ 
    16.             if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight) 
    17.             cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera 
    18.         }  
    19.     } 
    Javascript代码  收藏代码
    1. //Simple closure example 
    2. var list = document.getElementById('list').getElementsByTagName('li'); 
    3. for (var i=0; i<list.length; i++) 
    4.     list[i].onclick = (function(n){ 
    5.         return function (){ 
    6.             var li = this.parentNode.getElementsByTagName('li'); 
    7.             alert(li.length); 
    8.             alert(n);        
    9.         } 
    10.     })(i); 
    Java代码  收藏代码
    1. //common javascript code write copyright 
    2. copyright=new Date(); 
    3. update=copyright.getFullYear(); 
    4. document.write("&copy; " + update + " Artist Rise, All Rights Reserved."); 
    Javascript代码  收藏代码
    1. //JS 控制 随滚动条移动的层 
    2. <script language="javascript"> 
    3. var SP = {}; 
    4.  
    5. SP.getId = function(id){ 
    6.   return document.getElementById(id); 
    7. }; 
    8.  
    9. SP.sCenter = function(ele) { 
    10.   var docEle = document.documentElement, 
    11.       docBody = document.body, 
    12.       Ele = SP.getId(ele), 
    13.       Top = docEle.scrollTop+docBody.scrollTop+(docEle.clientHeight-Ele.offsetHeight)/2, 
    14.       Left = docEle.scrollLeft+(docEle.clientWidth-Ele.offsetWidth)/2; 
    15.       console.log(Ele.offsetHeight+','+docEle.clientHeight); 
    16.     Ele.style.top = Top+"px"; 
    17.     Ele.style.left = Left+"px"; 
    18. }; 
    19. SP.sLeftTop = function(ele){ 
    20.   var docEle = document.documentElement, 
    21.       docBody = document.body, 
    22.       Ele = SP.getId(ele), 
    23.       Top = docEle.scrollTop+docBody.scrollTop, 
    24.       Left = docEle.scrollLeft; 
    25.     Ele.style.top = Top+"px"; 
    26.     Ele.style.left = Left+"px";   
    27. }; 
    28. SP.sLeftBottom = function(ele) { 
    29.   var docEle = document.documentElement, 
    30.       docBody = document.body, 
    31.       Ele = SP.getId(ele), 
    32.       Top = docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight, 
    33.       Left = docEle.scrollLeft; 
    34.     Ele.style.top = Top+"px"; 
    35.     Ele.style.left = Left+"px"; 
    36. }; 
    37. SP.sRightTop = function(ele){ 
    38.   var docEle = document.documentElement, 
    39.       docBody = document.body, 
    40.       Ele = SP.getId(ele), 
    41.       Top = docEle.scrollTop+docBody.scrollTop, 
    42.       Left = docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth; 
    43.     Ele.style.top = Top+"px"; 
    44.     Ele.style.left = Left+"px";   
    45. }; 
    46. SP.sRightBottom = function(ele) { 
    47.   var docEle = document.documentElement, 
    48.       docBody = document.body, 
    49.       Ele = SP.getId(ele), 
    50.       Top = docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight, 
    51.       Left = docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth; 
    52.     Ele.style.top = Top+"px"; 
    53.     Ele.style.left = Left+"px"; 
    54. }; 
    55.  
    56. //init 
    57. function init(){ 
    58.   SP.sCenter('redcell'); 
    59.   SP.sLeftTop('redcell2'); 
    60.   SP.sLeftBottom('redcell3'); 
    61. }; 
    62.  
    63. window.onscroll = init; 
    64. window.onresize = init; 
    65. window.onload = init; 
    66. </script> 
  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    22. Generate Parentheses (backTracking)
    21. Merge Two Sorted Lists
    20. Valid Parentheses (Stack)
    19. Remove Nth Node From End of List
    18. 4Sum (通用算法 nSum)
    17. Letter Combinations of a Phone Number (backtracking)
    LeetCode SQL: Combine Two Tables
    LeetCode SQL:Employees Earning More Than Their Managers
  • 原文地址:https://www.cnblogs.com/Tracy-zdy/p/3817500.html
Copyright © 2011-2022 走看看