自动关闭浏览器窗口

时间:2014-11-01 06:49:30

标签: javascript html

我想在Firefox中自动关闭由 window.open()功能打开的窗口。我搜索了Stack Overflow论坛并找到了这些链接:

1)How do I automatically close a webpage? 2)Automatically Close current window and open new window with new URL address

但由于我不习惯使用JavaScript,因此无法正确实现代码。

请在下面找到打开窗口的代码 - 需要自动关闭。但是在生成的html文件中插入一个使其无效。

<script language="JavaScript" type="text/javascript">
function Clickheretoprint(vpid,vage,vroom,vname,vsex,vdoc,vdat)
{

  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
      disp_setting+="scrollbars=yes,width=300, height=200, left=100, top=25 resizable=yes";
  //var content_vlue = document.getElementById("T_PID").value;


   var docprint=window.open("","",disp_setting);
   docprint.document.open();
   docprint.document.write('<html>');
   docprint.document.write('<body style="margin:0.01in 0.01in 0.01in 0.01in; width:3in;height:2in;position:absolute;" onLoad="self.print()">');
   docprint.document.write('<table height="25%">');
docprint.document.write('<tr>');
   docprint.document.write('<td style=" font-weight: bold;" align="left">Regd : '+vpid+'&nbsp;&nbsp;&nbsp;Date :'+vdat+'</td>');
   docprint.document.write('</tr>');
   docprint.document.write('<tr>');
   docprint.document.write('<td style=" font-weight: bold;" align="left">Name : '+initCap(vname)+'</td>');
   docprint.document.write('</tr>');
   docprint.document.write('<tr>');
   docprint.document.write('<td colspan=2 align="left"; ><B>Age :</B>'+vage+'&nbsp;&nbsp;&nbsp;<B>Category :</B>'+vsex+'</td>');
   docprint.document.write('</tr>');
   docprint.document.write('<tr>');
   docprint.document.write('<td colspan=2 style="font-weight: bold;" >'+vdoc+' &nbsp;&nbsp;&nbsp;Room:'+vroom+'</td>');
   docprint.document.write('</tr>');
   docprint.document.write('</table>');
   docprint.document.write('<table height="25%">');
   docprint.document.write('<tr><td style="height:1in;width:100%;Font-family:CODE 39;"> <center>');
   docprint.document.write('<font face="CODE 39" size="7">');
   docprint.document.write('*'+vpid+'*');

   docprint.document.write('</font>');
   docprint.document.write('</center></td></tr>');
   docprint.document.write('</table>');
   docprint.document.write('</body></html>');
   docprint.document.close();
   docprint.focus();
}

请让我知道如何编辑生成的文件,以便在3秒后自动关闭。

谢谢!

PS:由于我对这个领域很陌生,所以我会非常感谢您的详细解答。

1 个答案:

答案 0 :(得分:0)

CODEPEN

试一试:

<button onclick="openWin()">Open and Close after 3s</button>
<script>
    var myWindow;
    function openWin() {
        myWindow = window.open("", "myWindow", "width=200, height=100");
        myWindow.document.write("<p>This is 'myWindow'</p>");
        setTimeout(closeWin, 3000);
    }

    function closeWin() {
        myWindow.close();
    }
</script>