zoukankan      html  css  js  c++  java
  • 超级实用且不花哨的js代码


    自动刷新

    <meta HTTP-EQUIV="refresh" C>

    禁止右键
    document.oncontextmenu = function() { return false;}

    //各种尺寸

    s  +=  "\r\n网页可见区域宽:"+  document.body.clientWidth;  
    s  +=  "\r\n网页可见区域高:"+  document.body.clientHeight;  
    s  +=  "\r\n网页可见区域高:"+  document.body.offsetWeight  +"  (包括边线的宽)";  
    s  +=  "\r\n网页可见区域高:"+  document.body.offsetHeight  +"  (包括边线的宽)";  
    s  +=  "\r\n网页正文全文宽:"+  document.body.scrollWidth;  
    s  +=  "\r\n网页正文全文高:"+  document.body.scrollHeight;  
    s  +=  "\r\n网页被卷去的高:"+  document.body.scrollTop;  
    s  +=  "\r\n网页被卷去的左:"+  document.body.scrollLeft;  
    s  +=  "\r\n网页正文部分上:"+  window.screenTop;  
    s  +=  "\r\n网页正文部分左:"+  window.screenLeft;  
    s  +=  "\r\n屏幕分辨率的高:"+  window.screen.height;  
    s  +=  "\r\n屏幕分辨率的宽:"+  window.screen.width;  
    s  +=  "\r\n屏幕可用工作区高度:"+  window.screen.availHeight;  
    s  +=  "\r\n屏幕可用工作区宽度:"+  window.screen.availWidth;  

    //正则匹配

    匹配中文字符的正则表达式: [\u4e00-\u9fa5]
    匹配双字节字符(包括汉字在内):[^\x00-\xff]
    匹配空行的正则表达式:\n[\s| ]*\r
    匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 
    匹配首尾空格的正则表达式:(^\s*)|(\s*$)(像vbscript那样的trim函数)
    匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
    匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
    以下是例子:
    利用正则表达式限制网页表单里的文本框输入内容:
    用 正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
    1.用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
    2.用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
    3.用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

    //无提示关闭

    function Close()
    {
    var ua=navigator.userAgent
    var ie=navigator.appName=="Microsoft Internet Explorer"?true:false
    if(ie)
    {
          var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE "))))
      if(IEversion< 5.5)
      {
       var str  = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">'
           str += '<param name="Command" value="Close"></object>';
           document.body.insertAdjacentHTML("beforeEnd", str);
           document.all.noTipClose.Click();
      }
          else
      {
           window.opener =null;
           window.close();
          }
       }
    else
    {
      window.close()
       }
    }

    //取得控件得绝对位置(1)

    <script language="javascript"> 
    function getoffset(e)

    var t=e.offsetTop; 
    var l=e.offsetLeft; 
    while(e=e.offsetParent)

      t+=e.offsetTop; 
      l+=e.offsetLeft; 

    var rec = new Array(1);
    rec[0]  = t;
    rec[1] = l;
    return rec

    </script>

    //获得控件的绝对位置(2)

    oRect = obj.getBoundingClientRect();
    oRect.left
    oRect.

    //屏蔽按键
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
      <noscript><meta http-equiv="refresh" content="0;url=about:noscript"></noscript>
      <title>屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键</title>
    </head>
    <body>
    <script language="Javascript"><!--
      //屏蔽鼠标右键、Ctrl+N、Shift+F10、F11、F5刷新、退格键
      //Author: meizz(梅花雨) 2002-6-18
    function document.oncontextmenu(){event.returnValue=false;}//屏蔽鼠标右键
    function window.onhelp(){return false} //屏蔽F1帮助
    function document.onkeydown()
    {
      if ((window.event.altKey)&&
          ((window.event.keyCode==37)||   //屏蔽 Alt+ 方向键 ←
           (window.event.keyCode==39)))   //屏蔽 Alt+ 方向键 →
      {
         alert("不准你使用ALT+方向键前进或后退网页!");
         event.returnValue=false;
      }
         /* 注:这还不是真正地屏蔽 Alt+ 方向键,
         因为 Alt+ 方向键弹出警告框时,按住 Alt 键不放,
         用鼠标点掉警告框,这种屏蔽方法就失效了。以后若
         有哪位高手有真正屏蔽 Alt 键的方法,请告知。*/
      if ((event.keyCode==8)  ||                 //屏蔽退格删除键
          (event.keyCode==116)||                 //屏蔽 F5 刷新键
          (event.ctrlKey && event.keyCode==82)){ //Ctrl + R
         event.keyCode=0;
         event.returnValue=false;
         }
      if (event.keyCode==122){event.keyCode=0;event.returnValue=false;}  //屏蔽F11
      if (event.ctrlKey && event.keyCode==78) event.returnValue=false;   //屏蔽 Ctrl+n
      if (event.shiftKey && event.keyCode==121)event.returnValue=false;  //屏蔽 shift+F10
      if (window.event.srcElement.tagName == "A" && window.event.shiftKey) 
          window.event.returnValue = false;             //屏蔽 shift 加鼠标左键新开一网页
      if ((window.event.altKey)&&(window.event.keyCode==115))             //屏蔽Alt+F4
      {
          window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");
          return false;
      }
    }
    </script>
    屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键
    </body>
    </html>

    //下载文件

    function DownURL(strRemoteURL,strLocalURL)
    {
    try
    {
      var xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP");
      xmlHTTP.open("Get",strRemoteURL,false);
      xmlHTTP.send();
      var adodbStream=new ActiveXObject("ADODB.Stream");
      adodbStream.Type=1;//1=adTypeBinary
      adodbStream.Open();
      adodbStream.write(xmlHTTP.responseBody);
      adodbStream.SaveToFile(strLocalURL,2);
      adodbStream.Close();
      adodbStream=null;
      xmlHTTP=null;
      
    }
    catch(e)
    {
      window.confirm("下载URL出错!");
    }
    //window.confirm("下载完成.");
    }

    //检验连接是否有效

    function getXML(URL) 
    {
    var xmlhttp = new ActiveXObject("microsoft.xmlhttp");
    xmlhttp.Open("GET",URL, false); 
    try

      xmlhttp.Send();
    }
    catch(e){}
    finally 
    {
      var result = xmlhttp.responseText;
      if(result) 
      {
       if(xmlhttp.Status==200)
       {
        return(true);
       }
       else 
       {
        return(false);
       }
      }
      else 
      {
       return(false);
      }
    }
    }

     

    //获得URL参数

    <body>
    <a href="javascript:location.href=location.href + '?a=1&b=2'">search</a>
    <script language="JavaScript">
    <!--
    var a = location.search.substr(1);
    if(a.length>0)
    {
    var re = /([^&]*?)\=([^&]*)/g
    var s = a.match(re);
    for(var i= 0;i<s.length;i++)
    {
      alert(s);
      alert(s.split("=")[1]);
    }
    }
    //-->
    </script>
    </body>

     

    //可编辑SELECT

    <input type=text name=re_name style="100px;height:21px;font-size:10pt;"><span style="18px;border:0px solid red;"><select name="r00" style="margin-left:-100px;118px; background-color:#FFEEEE;" onChange="document.all.re_name.value=this.value;">
                    <option value="1">11111111<option>
                    <option value="2">222222</option>
                    <option value="3">333333</option>
                  </select>
                  </span>

     

     //操作EXECL

    <script language="javascript">
    function jStartExcel() {
    var xls = new ActiveXObject ( "Excel.Application" );
    xls.visible = true;
    var newBook = xls.Workbooks.Add;
    newBook.Worksheets.Add;
    newBook.Worksheets(1).Activate;
    xls.ActiveWorkBook.ActiveSheet.PageSetup.Orientation = 2;
    xls.ActiveWorkBook.ActiveSheet.PageSetup.PaperSize = 5;
    newBook.Worksheets(1).Columns("A").columnwidth=50;
    newBook.Worksheets(1).Columns("A").WrapText = true;
    newBook.Worksheets(1).Columns("B").columnwidth=50;
    newBook.Worksheets(1).Columns("B").WrapText = true;
    newBook.Worksheets(1).Range("A1:B1000").NumberFormat = "0";
    newBook.Worksheets(1).Range("A1:B1000").HorizontalAlignment = -4131;
    newBook.Worksheets(1).Cells(1,1).Interior.ColorIndex="15";
    newBook.Worksheets(1).Cells(1,1).value="First Column, First Cell";
    newBook.Worksheets(1).Cells(2,1).value="First Column, Second Cell";
    newBook.Worksheets(1).Cells(1,2).value="Second Column, First Cell";
    newBook.Worksheets(1).Cells(2,2).value="Second Column, Second Cell";
    newBook.Worksheets(1).Name="My First WorkSheet";
    }
    </script>

    //自定义提示条

    <a href="#" title="这是提示">tip</a>
    <script Language="JavaScript">
    //***********默认设置定义.*********************
    tPopWait=50;//停留tWait豪秒后显示提示。
    tPopShow=5000;//显示tShow豪秒后关闭提示
    showPopStep=20;
    popOpacity=99;
    //***************内部变量定义*****************
    sPop=null;
    curShow=null;
    tFadeOut=null;
    tFadeIn=null;
    tFadeWaiting=null;
    document.write("<style type='text/css'id='defaultPopStyle'>");
    document.write(".cPopText {  background-color: #F8F8F5;color:#000000; border: 1px #000000 solid;font-color: font-size: 12px; padding-right: 4px; padding-left: 4px; height: 20px; padding-top: 2px; padding-bottom: 2px; filter: Alpha(Opacity=0)}");
    document.write("</style>");
    document.write("<div id='dypopLayer' style='position:absolute;z-index:1000;' class='cPopText'></div>");

    function showPopupText(){
    var o=event.srcElement;
    MouseX=event.x;
    MouseY=event.y;
    if(o.alt!=null && o.alt!=""){o.dypop=o.alt;o.alt=""};
            if(o.title!=null && o.title!=""){o.dypop=o.title;o.title=""};
    if(o.dypop!=sPop) {
    sPop=o.dypop;
    clearTimeout(curShow);
    clearTimeout(tFadeOut);
    clearTimeout(tFadeIn);
    clearTimeout(tFadeWaiting);
    if(sPop==null || sPop=="") {
    dypopLayer.innerHTML="";
    dypopLayer.style.filter="Alpha()";
    dypopLayer.filters.Alpha.opacity=0;
    }
    else {
    if(o.dyclass!=null) popStyle=o.dyclass 
    else popStyle="cPopText";
    curShow=setTimeout("showIt()",tPopWait);
    }
    }
    }
    function showIt(){
    dypopLayer.className=popStyle;
    dypopLayer.innerHTML=sPop;
    popWidth=dypopLayer.clientWidth;
    popHeight=dypopLayer.clientHeight;
    if(MouseX+12+popWidth>document.body.clientWidth) popLeftAdjust=-popWidth-24
    else popLeftAdjust=0;
    if(MouseY+12+popHeight>document.body.clientHeight) popTopAdjust=-popHeight-24
    else popTopAdjust=0;
    dypopLayer.style.left=MouseX+12+document.body.scrollLeft+popLeftAdjust;
    dypopLayer.style.top=MouseY+12+document.body.scrollTop+popTopAdjust;
    dypopLayer.style.filter="Alpha(Opacity=0)";
    fadeOut();
    }
    function fadeOut(){
    if(dypopLayer.filters.Alpha.opacity<popOpacity) {
    dypopLayer.filters.Alpha.opacity+=showPopStep;
    tFadeOut=setTimeout("fadeOut()",1);
    }
    else {
    dypopLayer.filters.Alpha.opacity=popOpacity;
    tFadeWaiting=setTimeout("fadeIn()",tPopShow);
    }
    }
    function fadeIn(){
    if(dypopLayer.filters.Alpha.opacity>0) {
    dypopLayer.filters.Alpha.opacity-=1;
    tFadeIn=setTimeout("fadeIn()",1);
    }
    }
    document.onmouseover=showPopupText;
    </script>

     

    //判断键值

    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <head>
    <script language="javascript">
    var ie  =navigator.appName=="Microsoft Internet Explorer"?true:false;
      
    function keyDown(e)
    {
    if(!ie)
    {
      var nkey=e.which;
      var iekey='现在是ns浏览器';
      var realkey=String.fromCharCode(e.which);
    }
    if(ie)
    {
      var iekey=event.keyCode;
      var nkey='现在是ie浏览器';
      var realkey=String.fromCharCode(event.keyCode);
      if(event.keyCode==32){realkey='\' 空格\''}
      if(event.keyCode==13){realkey='\' 回车\''}
      if(event.keyCode==27){realkey='\' Esc\''}
      if(event.keyCode==16){realkey='\' Shift\''}
      if(event.keyCode==17){realkey='\' Ctrl\''}
      if(event.keyCode==18){realkey='\' Alt\''}
    }
    alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
    }
    document.onkeydown = keyDown;
    </script>
    </head>
    <body>
    //Javascript Document.
    <hr>
    <center>
    <h3>请按任意一个键。。。。</h3>
    </center>
    </body>
    </html>

     

    //图象按比例

    <script language="JavaScript">
    <!--
    //图片按比例缩放
    var flag=false;
    function DrawImage(ImgD){
    var image=new Image();
    var iwidth = 80;  //定义允许图片宽度
    var iheight = 80;  //定义允许图片高度
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= iwidth/iheight){
      if(image.width>iwidth){  
      ImgD.width=iwidth;
      ImgD.height=(image.height*iwidth)/image.width;
      }else{
      ImgD.width=image.width;  
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }
    else{
      if(image.height>iheight){  
      ImgD.height=iheight;
      ImgD.width=(image.width*iheight)/image.height;  
      }else{
      ImgD.width=image.width;  
      ImgD.height=image.height;
      }
      ImgD.alt=image.width+"×"+image.height;
      }
    }

    //-->
    </script>
    <img src=".." onload = "DrawImage(this)">

     

     

  • 相关阅读:
    java身份证号码校验、邮箱校验、手机号码/电话号码校验
    垃圾回收器的基本原理是什么?垃圾回收器可以马上回收内存吗?有什么办法主动通知虚拟机进行垃圾回
    redis缓存机制和底层实现
    java自然语言StanfordCoreNLP入门
    java生成汉字集
    maven打包 invalid entry size Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.14.RELEASE:repackage (default) on project
    maven解决大项目打包慢的问题
    visualVM远程监控jetty
    jetty配置远程debug
    图数据库ubentu环境neo4j安装
  • 原文地址:https://www.cnblogs.com/nianshi/p/1617006.html
Copyright © 2011-2022 走看看