<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        <title>弹窗代码</title>
        <style type="text/css">
            td {font-size: 12px;}
        </style>
  <SCRIPT LANGUAGE="JavaScript" >
function addEventSimple(obj,evt,fn) {
if (obj.addEventListener)
obj.addEventListener(evt,fn,false);
else if (obj.attachEvent)
obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
if (obj.removeEventListener)
obj.removeEventListener(evt,fn,false);
else if (obj.detachEvent)
obj.detachEvent('on'+evt,fn);
}
function fuyouPopUp(targetUrl){
  var w=screen.availWidth;
  var h=screen.availHeight;
  var casalef='width='+w+',height='+h+',toolbar=1,location=1,titlebar=1,menubar=1,scrollbars=1,resizable=1,directories=1,status=1';
      var l = 0 ;
      var t = 0 ;
  var i=window.open("about:blank","_backad",casalef+",left="+l+",top="+t);
  i.blur(); 
i.opener.focus(); 
i.location=targetUrl;
}
function fuyouClickPopup(targetURl){
addEventSimple(document,'click',function (){
 fuyouPopUp(targetURl);
})

}
function fuyouWinOpen(url){
try{
fuyouPopUp(url);
}catch(e){
fuyouClickPopup(url)
}finally{
document.onclick = null;
}
}
function fuyouPop (url){
  fuyouWinOpen(url);
    }
       fuyouPop ('http://www.sina.com');
        </SCRIPT>
      
   
       
    </head>
<body>
 cpm
<a href="http://www.baidu.com">baidu</a>
</body></html>我只想弹出一次,就把不弹了,可我 document.onclick = null;
把事件取消注册,点击还依然会弹出,这是什么,要怎么改进

解决方案 »

  1.   

    楼主的代码 和所提问题没挂钩嘛,没看明白,你的onclick在哪?
      

  2.   

    在这函数里啊function fuyouClickPopup(targetURl){
            addEventSimple(document,'click',function (){
                 fuyouPopUp(targetURl);
            })
        
    }
      

  3.   

    removeEventSimple就是移走事件的函数。function fuyouClickPopup(targetURl){
    window.documentClick = function (){
    fuyouPopUp(targetURl);
    }
    addEventSimple(document,'click', documentClick);
    }
    function fuyouWinOpen(url){
        try{
            fuyouPopUp(url);
        }catch(e){
            fuyouClickPopup(url)
        }finally{
            removeEventSimple(document, 'click', window.documentClick);
    }
    }
    function fuyouPop (url){
              fuyouWinOpen(url);
        }
           fuyouPop ('http://www.sina.com');
      

  4.   

    需要用removeEventListener方法移除事件的处理函数
      

  5.   

    可那个带参数的,要怎么移除直接onclick = nulll不能移除事件吗 
      

  6.   

    removeEventListener是确保在按钮的时间响应时不触发,如这里的
     removeEventSimple(document, 'click', window.documentClick);
    就是保证在点击的时候不触发onclick,实际上这里也可以写为onclick="javascript:return false;"即可你想移除按钮本身的onclick,当然不行啊,
      

  7.   

    既然自己写了attach和detach的方法,还问这个问题...
      

  8.   

    function fuyouClickPopup(targetURl){
           /*
            addEventSimple(document,'click',function (){
                 fuyouPopUp(targetURl);        })*/
            
           addEventSimple(document,'click',myclick);
           function myclick(){
                 fuyouPopUp(targetURl);
                   function removeEventSimple(document,'click',myclick)
            }
        
    }
      

  9.   


    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
            <title>弹窗代码</title>
            <style type="text/css">
                td {font-size: 12px;}
            </style>
      <SCRIPT LANGUAGE="JavaScript" >
        function addEventSimple(obj,evt,fn) {
            if (obj.addEventListener)
                obj.addEventListener(evt,fn,false);
            else if (obj.attachEvent)
                obj.attachEvent('on'+evt,fn);
        }
        
        function removeEventSimple(obj,evt,fn) {
            if (obj.removeEventListener)
                obj.removeEventListener(evt,fn,false);
            else if (obj.detachEvent)
                obj.detachEvent('on'+evt,fn);
        }
    function fuyouPopUp(targetUrl){
          var w=screen.availWidth;
          var h=screen.availHeight;
          var casalef='width='+w+',height='+h+',toolbar=1,location=1,titlebar=1,menubar=1,scrollbars=1,resizable=1,directories=1,status=1';
          var l = 0 ;
          var t = 0 ;
          var i=window.open("about:blank","_backad",casalef+",left="+l+",top="+t);
             i.blur(); 
            i.opener.focus(); 
            i.location=targetUrl;
    }
    function fuyouClickPopup(targetURl){ 
          /* 
            addEventSimple(document,'click',function (){ 
                fuyouPopUp(targetURl);         })*/ 
            
          addEventSimple(document,'click',myclick); 
          function myclick(){ 
                fuyouPopUp(targetURl); 
                  function removeEventSimple(document,'click',myclick) 
            } 
        

    function fuyouWinOpen(url){
        try{
            fuyouPopUp(url);
        }catch(e){
            fuyouClickPopup(url)
        }finally{
            document.onclick = null;
            }
    }
    function fuyouPop (url){
              fuyouWinOpen(url);
        }
           fuyouPop ('http://www.sina.com');
            </SCRIPT>
          
       
           
        </head>
    <body>
     cpm
    <a href="http://www.baidu.com">baidu</a>
    </body></html>我改成这样啊,都没有弹出,用firebug 调试,也没发现错误