【问题标题】:Disable a pop up link for 5 seconds using javascript使用 javascript 禁用弹出链接 5 秒
【发布时间】:2012-10-18 11:45:42
【问题描述】:

对于一个项目,我需要使用 PHP 代码打印一个文档。 目前我有一个自动关闭弹出窗口来开始打印。

我遇到的唯一问题是用户可能会向按钮发送垃圾邮件,从而创建大量打印请求和庞大的队列。

我现在的代码:

function newPopup(url) {
popupWindow = window.open(
url,'popUpWindow','height=10,width=100,left=10,top=10,resizable=no,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=no');    // Verstop op achtergrond
popupWindow.blur();
}
<a href="JavaScript:newPopup('print.php');">Print</a>

我找到了一些停止链接的代码,但我在实现这些代码时遇到了问题,因为我已经将其称为弹出窗口。

【问题讨论】:

    标签: javascript jquery printing popup disable-link


    【解决方案1】:

    您可以使用标志:

    var flag=true;
    function newPopup(url) {
      if(flag) {
        window.open(...).blur();
        flag=false;
        window.setTimeout(function(){flag=true;},5*1000);
      }
    }
    

    不是一个“好”的解决方案(使用全局变量),但它应该可以工作。

    【讨论】:

      【解决方案2】:

      您可以在打开弹出窗口之前禁用链接,然后在五秒钟后重新启用它。 问题是不能以非常便携的方式启用/禁用链接。要解决此问题,您必须保存实际链接,将其替换为假链接,然后稍后重新启用(当间隔过去时)。像这样:

      function newPopup(url) {
          // Save current link and replace it with a fake one
          var oldLink = $("#linkid").attr("href");
          $("#linkid").attr("href", "#");
      
          setinterval(function() {
              // Restore true link
              $("#linkid").attr("href", oldLink);
          }, 5000);
      
          // ...
      }
      

      您可以将此代码提取到一个单独的函数temporaryDisableLink(id, timeout) 以将其用于许多不同的链接(不会污染所有其他代码)。

      现在让我们探索其他解决方案。

      您的 HTML 代码必须更新为(如果您想对许多链接重复使用相同的函数,否则您不需要传递链接 id 参数)到:

      <a id="link-print" 
         href="JavaScript:newPopup('#link-print', 'print.php');">
         Print
      </a>
      

      IE(和 Opera)不支持 pointer-events CSS 属性,所以我不建议在现实世界中使用它。反正就是:

      function newPopup(id, url) {
          $(id).css("pointer-events", "none");
      
          setinterval(function() {
              $(id).css("pointer-events", "auto");
          }, 5000);
      
          // ...
      }
      

      因为您使用 JavaScript 打开弹出窗口,您可以考虑稍微更改函数以使用 custom disabled 属性(或者检查 pointer-events,如果您计划一起使用):

      function newPopup(id, url) {
          if ($(id).attr("disabled") == "disabled") {
              return false;
          }
      
          $(id).attr("disabled", "disabled");
          setinterval(function() {
              $(id).removeAttr("disabled");
          }, 5000);
      
          // ...
      }
      

      【讨论】:

        【解决方案3】:
        <script>
            function newPopup(url) {
                setTimeout(function () {
                    popupWindow = window.open(
        url, 'popUpWindow', 'height=10,width=100,left=10,top=10,resizable=no,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=no');    // Verstop op achtergrond
                    popupWindow.blur();
                },5000
                );
            }
        </script>
        <a href="JavaScript:newPopup('print.php');">Print</a>
        

        【讨论】:

          猜你喜欢
          • 2011-06-03
          • 2018-05-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-05
          • 1970-01-01
          • 2019-04-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多