JavaScript 常用语句总结


          最近做了一个工资管理系统,大部分采用asp.net 2.0提供的控件发现虽然方便,但还是有好多地方是一定要采用JavaScript才能实现功能的。。
         对于这个项目常用到的一些脚本我做了如下总结:
     1。。//执行客户端脚本,makeSure()是客户段的脚本函数:
             ClientScript.RegisterStartupScript(this.GetType(), "", "<script>makeSure()</script>");
             相当于:Response.Write("<script>alert('注销失败!')</script>"); 但也有一点区别吧。。
             下面是一个从cs页面传值到aspx页面的调用脚本函数:
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setFirstPage(" + searchYear + ")</script>");//执行客户端脚本

    
 2。。//为Button1添加脚本属性,此句要在 form_load()事件中哟。。相关可参见我的关于用法的一篇随笔:
           《Button1.Attributes.Add()方法小结
             this.Button1.Attributes.Add("onclick", "return checkWorker()");

     3。。//下面这个实列也是一个很有用的实列,它是在当前document对像中通过一个button 的 click事件
             得到一个listbox中的值,然后用这个值作为参数,将此documetn中的一个iframe重定向到一个新页面:
             <input type="button" value="选择此注册号》"  runat ="server" onclick="document.getElementById('registuser').src='regist1.aspx?uid='+document.getElementById('ListBox1').options[document.getElementById('ListBox1').selectedIndex].value" id="Button2" visible="false">
            简单一点的用法就是:
             <input type ="button"  value ="创建新用户>>" id ="createNewUser" runat="server" visible="false" onclick ="document.getElementById('registuser').src='regist2.aspx'" />

      4。。//如果当前窗口在一个父窗口的子窗口中,(就是iframe中嘛)。想让父窗口重定向到一个新页面的脚本是:
           <script  language ="javascript" type ="text/javascript" >
     function registSuccess()
     {
        alert("添加成功!");
        parent.location.href ='regist.aspx';
     }
    
     function registFail()
     {
        alert("添加失败!");
        parent.location.href ='regist.aspx';
     }
    </script>
    PS :此函数可由一个button的click引发。很有用的。。。

     5。。在aspx中写cs代码方法:
        以下代码写在<head></head>中:
        <script runat ="server">
        public string  getmyYear()
        {
            return DateTime.Now.Year.ToString();
        }
     </script>


      以下代码写在<body></body>注:以下的格式是固定的:
         <a href ="view1.aspx?year=<%=getmyYear()%>" target ="text">连接的文本或图像<a>
    
    6。。设置当前 iframe 对像中的src值:
             function setFirstPage(Obj)
{
   document.getElementById('text').src="content/view1.aspx?year="+Obj;
}
  

        7。。。以下是禁止页面上使用鼠标右键代码:   

<head>
<script language="javascript">
  function myevent()
  {
    
     window.event.returnValue=false;
 
  }
</script>
</head>

<body oncontextmenu="myevent()">

</body>

//PS:body中也可以这样写,这样不用调用事件:
 <body oncontextmenu="return false">
//PSTO:还有一种方式就是不用在body中写任何事件,只需要在script标签对中写就可以了:

<head> 

<script language="javascript">
  document.oncontextmenu=myevent;
  function myevent()
  {
    
     window.event.returnValue=false;
 
  }

</script>

</head>
    

8  如何隐藏与出现浏览器右侧的滚动条

   document.documentElement.style.overflow='hidden'; //隐藏

    document.documentElement.style.overflow='scroll';  //出现

9  如何取消与激活鼠标滚动事件:

    //设置鼠标滚动事件无效

    if(document.addEventListener){
      document.addEventListener("DOMMouseScroll",function(e){
       e.preventDefault();
      },false);
     }else{
      document.onmousewheel = function(){
       event.returnValue = false; //设置鼠标滚动事件无效
      };
     }

    //   设置鼠标滚动事件有效

    if(document.addEventListener){
      document.addEventListener("DOMMouseScroll",function(e){
       e.preventDefault();
      },false);
     }else{
      document.onmousewheel = function(){
       event.returnValue = true; //设置鼠标滚动事件有效
      };
     }

10   IE6、IE7、Firefox无提示关闭窗口的代码 :

IE6:
<script>
window.opener=null;
window.close();
</script>
IE7:
<script>
window.open('','_self');
window.close();
</script>
IE6、IE7、FF通用代码:
<script>
window.opener=null;
window.open('','_self');
window.close();
</script>

11  JS回避F5键
    <script language="javascript">

    document.onkeydown=mykeydown;

    function   mykeydown()
    {
        if(event.keyCode==116) //屏蔽F5刷新键
        {
            window.event.keyCode=0;
            return   false;
        }
    }

 </script>

12 ......


       

原文地址:https://www.cnblogs.com/wantingqiang/p/1211354.html