如何关闭通过单击javascript中的图像已打开的新标签页?

时间:2018-11-22 06:30:56

标签: javascript html5

我已经用html编写了代码,可在单击图像后在新选项卡中打开链接,但是问题是我无法关闭该新窗口。

我想知道如何关闭新窗口?

<html>
<h1>Click on the image below to open google homepage </h1>

<head>
    <script>
        var x;
        var win = document.links;

        function closeFunc() {
            win[0].close();
        }
    </script>
</head>

<body>
    <a id="google" href="http://www.google.com" target="_blank">
        <img src="https://usercontent1.hubstatic.com/12498898_f520.jpg" alt="image" />
    </a>
    <br> <br>
    <button onclick="closeFunc()"> Close</button>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

只需尝试

<html>
<h1>Click on the image below to open google homepage </h1>

<head>
    <script>
        var x;
        var win = document.links;

        function closeFunc() {
            win[0].close();
        }


           function popuponclick()
   {
      my_window = window.open("http://www.google.com",
       "mywindow");

   }

   function closepopup()
   {
      if(false == my_window.closed)
      {
         my_window.close ();
      }
      else
      {
         alert('Window already closed!');
      }
   }
    </script>
</head>

<body>
    <a id="google" href="javascript: popuponclick()" target="_blank">
        <img src="https://usercontent1.hubstatic.com/12498898_f520.jpg" alt="image" />
    </a>
    <br> <br>
    <button onclick="javascript: closepopup()"> Close</button>
</body>

</html>