【问题标题】:How to open link in new tab and close it immediately after page load?如何在新标签页中打开链接并在页面加载后立即关闭它?
【发布时间】:2020-03-07 14:30:33
【问题描述】:

我在一个页面上有两个链接。当我单击第一个链接时,我希望链接在新选项卡中打开并在页面加载后立即关闭。我希望第二个链接也发生同样的情况。

另外,我怎样才能将这个步骤自动化 'n' 次?

【问题讨论】:

    标签: javascript html web


    【解决方案1】:

    你可以试试这个功能:

    function openUrl(url, title) {
        if (!title) {
            title = 'Just another window';
        }
        var x = window.open(url, title, 'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
    
        x.blur();
        x.close();
    }
    

    此函数接受两个参数:URL(新窗口的 URL)和标题(将在新创建的窗口上显示的标题)。

    【讨论】:

      【解决方案2】:
      <html>
      <head>
      <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
      </head>
      <body>
      
      <form action="http://google.ca" id='idForm'>
            <div>
              <button type='submit' >post</button>
            </div>
           </form>
        </body>  
      <script>
      
          $("#idForm").submit(function(e) {
      
              var form = $(this);
              var url = form.attr('action');
      
              $.ajax({
                     type: "POST",
                     url: url,
                     data: form.serialize(), // serializes the form's elements.
                     success: function(data)
                     {
                         alert(data); // show response from the php script.
                     }
                   });
      
              e.preventDefault(); // avoid to execute the actual submit of the form.
          });
      
      </script>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-29
        相关资源
        最近更新 更多