script – cancel javascript event in IE7 & FF

來源:互聯網
上載者:User
    The following code works correctly in IE7, FF2.0, Opera9.21,
and Safari3.01, it fires the server event only when the user confirm to delete in
the browser.
    the server control:    <asp:LinkButton ID="cmdDelete" runat="server" OnClick="cmdDelete_Click"
        Text="Delete" CssClass="linkbutton" OnClientClick="javascript:deleteConfirm(event);" />

    the javascript function:function deleteConfirm(e)
{
    if(!confirm("Are you sure to delete the selected items?")){
        if(window.event &&
window.event.returnValue){
//for IE
            window.event.cancelBubble=true;
            window.event.returnValue=false; 
        }
        if(e && e.preventDefault){ //for FF
            e.preventDefault();
            e.stopPropagation();
        }
    }
}  

    no test performed in IE6.

    There's another way to achieve this goal, see the following code(As
Hallvord pointed out in the comments, the following code is ugly, nonstandard, and
has compatibility problems. But I got this code by Google, and the original author
means a way that no necessary to provider any event arguments for the function deleteConfirm().
I just found difficult to approach this and the advice for you is: do not use it
in your application!):function deleteConfirm()
{
    if(!confirm("Are you sure to delete the selected items?")){
        var e=getEvent();
         if(window.event){
            e.returnValue=false;
            e.cancelBubble=true;
          }else if(e.preventDefault){
            e.preventDefault();
            e.stopPropagation();
         } 
     }

function getEvent(){
     if(window.event)    {return window.event;}
     func=getEvent.caller;            
     while(func!=null){    
         var arg0=func.arguments[0];
         if(arg0){
             if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
                 || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
                 return arg0;
             }
         }
         func=func.caller;
     }
     return null;

    The server control is like below, this time, no event argument
specified in the javascript function call in OnClientClick event.<asp:LinkButton ID="cmdDelete" runat="server" OnClick="cmdDelete_Click"
        Text="Delete" CssClass="linkbutton" OnClientClick="javascript:deleteConfirm();" />

    It works well in IE7, FF2.0, Opera9.21 (not include Safari3.01).

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.