大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > JavaScript技巧 > 一些比较实用的javascript方法收集

一些比较实用的javascript方法收集

关键词:javascript常用方法  阅读(622) 赞(14)

[摘要]本文是对一些比较实用的javascript方法收集的讲解,对学习JavaScript编程技术有所帮助,与大家分享。

动态加载javascript文件

Javascript代码
  1. <scripttype="text/javascript">
  2. //<!--
  3. /*动态加载方法*/
  4. functionloadScript(url,callback){
  5. varscript=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("Allfilesareloaded!");
  32. });
  33. });
  34. });
  35. //-->
  36. </script>



Java代码
  1. functionGetXmlHttpReuqest(){
  2. varxmlHttp=null;
  3. try
  4. {
  5. xmlHttp=newXMLHttpRequest();//对于firefoxchrome等浏览器
  6. }
  7. catch(e)
  8. {
  9. try
  10. {
  11. xmlHttp=newActiveXObject("Msxml2.XMLHTTP");//对于IE浏览器
  12. }
  13. catch(e)
  14. {
  15. try
  16. {
  17. xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. catch(e)
  20. {
  21. xmlHttp=false;
  22. }
  23. }
  24. }
  25. returnfalse;
  26. }



Javascript代码
  1. //js判断浏览器
  2. <script>
  3. window["MzBrowser"]={};
  4. (function(){
  5. if(MzBrowser.platform)return;
  6. varua=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)varre=/Firefox(\s|\/)(\d+(\.\d+)?)/;
  15. elseif(MzBrowser.ie)varre=/MSIE()(\d+(\.\d+)?)/;
  16. elseif(MzBrowser.opera)varre=/Opera(\s|\/)(\d+(\.\d+)?)/;
  17. elseif(MzBrowser.netscape)varre=/Netscape(\s|\/)(\d+(\.\d+)?)/;
  18. elseif(MzBrowser.safari)varre=/Version(\/)(\d+(\.\d+)?)/;
  19. elseif(MzBrowser.mozilla)varre=/rv(\:)(\d+(\.\d+)?)/;
  20. if("undefined"!=typeof(re)&&re.test(ua))
  21. MzBrowser.version=parseFloat(RegExp.$2);
  22. })();
  23. functionabc()
  24. {
  25. if(MzBrowser.ie)
  26. {
  27. alert('ie');
  28. }
  29. if(MzBrowser.firefox)
  30. {
  31. alert('firefox');
  32. }
  33. alert(MzBrowser.version);
  34. }
  35. abc();
  36. </script>
  37. </head>
  38. <body>
  39. <hr>
  40. <scripttype="text/javascript">
  41. varbrowser=navigator.appName;
  42. varb_version=navigator.appVersion;
  43. varversion=parseFloat(b_version);
  44. document.write("Browsername:"+browser);
  45. document.write("<br>");
  46. document.write("Browserversion:"+version);
  47. </script>
  48. <hr>
  49. <scripttype="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("Cookiesenabled:");
  59. document.write(navigator.cookieEnabled+"");
  60. document.write("Browser'suseragentheader:");
  61. document.write(navigator.userAgent+"");
  62. </script>
  63. <hr>
  64. <scripttype="text/javascript">
  65. varx=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. <scripttype="text/javascript">
  93. varbrowser=navigator.appName;
  94. varb_version=navigator.appVersion;
  95. varversion=parseFloat(b_version);
  96. if((browser=="Netscape"||browser=="MicrosoftInternetExplorer")&&(version>=4))
  97. {
  98. document.write("Yourbrowserisgoodenough!");
  99. }else{
  100. document.write("It'stimetoupgradeyourbrowser!");
  101. }
  102. </script>
代码
  1. //始终将divTop置于窗口的顶部
  2. <scripttype="text/javascript">
  3. <!--
  4. window.onscroll=function(){
  5. vardivTop=document.getElementById("divTop");
  6. divTop.style.top=document.body.scrollTop;
  7. }
  8. //-->
  9. </script>
  10. //htmlcode
  11. <divid="divTop"style="position:absolute;top:0px;right:0px;color:#cccccc;width:100px;height:20px;backgrond-colot:naty;font-size:12px;">divTop</div>
Javascript代码
  1. <scripttype="text/javascript">
  2. //<![CDATA[
  3. (function(w){
  4. alert('loadstartturn.');
  5. })(window);
  6. //监听浏览器
  7. varMSG_UNLOAD="确定离开?";
  8. varUnloadConfirm={};
  9. //启用监听浏览器刷新、关闭的方法
  10. UnloadConfirm.set=function(confirm_msg){
  11. window.onbeforeunload=function(event){
  12. event=event||window.event;
  13. event.returnValue=confirm_msg;
  14. }
  15. }
  16. //关闭监听浏览器刷新、关闭的方法
  17. UnloadConfirm.clear=function(){
  18. window.onbeforeunload=function(){};
  19. }
  20. UnloadConfirm.set(MSG_UNLOAD);
  21. //]]-->
  22. </script>
Javascript代码
  1. //只能输入数字和小数点
  2. functioncheckkey(value,e){
  3. varkey=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. }elseif(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. *@create11/29/2010
  4. */
  5. functioncheck_check(checkname,message,msgid,ML){
  6. varflag;
  7. varchecks=document.getElementsByName(checkname);
  8. for(vari=0;i<checks.length;i++){
  9. if(checks[i].checked==true){
  10. flag=true;
  11. returntrue;
  12. break;
  13. }
  14. }
  15. if(!flag){
  16. //alert(message);
  17. varmsgid=document.getElementById(msgid);
  18. varmsgObj=document.createElement("div");
  19. msgObj.innerHTML=message;
  20. msgObj.style.marginLeft=ML;
  21. msgObj.style.color="#f00";
  22. msgid.appendChild(msgObj);
  23. returnfalse;
  24. }
  25. }
Javascript代码
  1. /**
  2. *iframe自适应高度代码
  3. *
  4. */
  5. functioniFrameHeight(id){
  6. varcwin=document.getElementById(id);
  7. if(document.getElementById)
  8. {
  9. if(cwin&&!window.opera){
  10. if(cwin.contentDocument&&cwin.contentDocument.body.offsetHeight){
  11. cwin.height=cwin.contentDocument.body.offsetHeight;
  12. }
  13. elseif(cwin.Document&&cwin.Document.body.scrollHeight){
  14. cwin.height=cwin.Document.body.scrollHeight;
  15. }
  16. }else{
  17. if(cwin.contentWindow.document&&cwin.contentWindow.document.body.scrollHeight)
  18. cwin.height=cwin.contentWindow.document.body.scrollHeight;//Opera
  19. }
  20. }
  21. }
Javascript代码
  1. //Simpleclosureexample
  2. varlist=document.getElementById('list').getElementsByTagName('li');
  3. for(vari=0;i<list.length;i++)
  4. {
  5. list[i].onclick=(function(n){
  6. returnfunction(){
  7. varli=this.parentNode.getElementsByTagName('li');
  8. alert(li.length);
  9. alert(n);
  10. }
  11. })(i);
  12. }



Java代码
  1. //commonjavascriptcodewritecopyright
  2. copyright=newDate();
  3. update=copyright.getFullYear();
  4. document.write("&copy;"+update+"ArtistRise,AllRightsReserved.");




Javascript代码
  1. //JS控制随滚动条移动的层
  2. <scriptlanguage="javascript">
  3. varSP={};
  4. SP.getId=function(id){
  5. returndocument.getElementById(id);
  6. };
  7. SP.sCenter=function(ele){
  8. vardocEle=document.documentElement,
  9. docBody=document.body,
  10. Ele=SP.getId(ele),
  11. Top=docEle.scrollTop+docBody.scrollTop+(docEle.clientHeight-Ele.offsetHeight)/2,
  12. Left=docEle.scrollLeft+(docEle.clientWidth-Ele.offsetWidth)/2;
  13. console.log(Ele.offsetHeight+','+docEle.clientHeight);
  14. Ele.style.top=Top+"px";
  15. Ele.style.left=Left+"px";
  16. };
  17. SP.sLeftTop=function(ele){
  18. vardocEle=document.documentElement,
  19. docBody=document.body,
  20. Ele=SP.getId(ele),
  21. Top=docEle.scrollTop+docBody.scrollTop,
  22. Left=docEle.scrollLeft;
  23. Ele.style.top=Top+"px";
  24. Ele.style.left=Left+"px";
  25. };
  26. SP.sLeftBottom=function(ele){
  27. vardocEle=document.documentElement,
  28. docBody=document.body,
  29. Ele=SP.getId(ele),
  30. Top=docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight,
  31. Left=docEle.scrollLeft;
  32. Ele.style.top=Top+"px";
  33. Ele.style.left=Left+"px";
  34. };
  35. SP.sRightTop=function(ele){
  36. vardocEle=document.documentElement,
  37. docBody=document.body,
  38. Ele=SP.getId(ele),
  39. Top=docEle.scrollTop+docBody.scrollTop,
  40. Left=docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth;
  41. Ele.style.top=Top+"px";
  42. Ele.style.left=Left+"px";
  43. };
  44. SP.sRightBottom=function(ele){
  45. vardocEle=document.documentElement,
  46. docBody=document.body,
  47. Ele=SP.getId(ele),
  48. Top=docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight,
  49. Left=docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth;
  50. Ele.style.top=Top+"px";
  51. Ele.style.left=Left+"px";
  52. };
  53. //init
  54. functioninit(){
  55. SP.sCenter('redcell');
  56. SP.sLeftTop('redcell2');
  57. SP.sLeftBottom('redcell3');
  58. };
  59. window.onscroll=init;
  60. window.onresize=init;
  61. window.onload=init;
  62. </script>


相关评论