使用Javascript/jQuery在当前窗口后面打开一个窗口

Open a window behind the current window using Javascript/jQuery

本文关键字:窗口 一个 Javascript jQuery 使用      更新时间:2023-09-26

我想在点击时打开一个窗口,但我想在当前窗口后面打开它,或者当新窗口打开时,它应该最小化自己。我做了一个函数,但它实际上不起作用。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function wn(){
  var mm=window.open('http://www.google.com','newwindow','width=200, height=200', "_blank");
}
</script>
</head>
<body>
<a href="#" onclick="wn()">click</a>
</body>

编辑

父窗口HTML

<script language="JavaScript" type="text/javascript">
function openPopUP() {
  window.open('mywindow.html','NewWin',
            'toolbar=no,status=no,width=350,height=135')
}
</script>
<body onLoad="openPopUP();">
or
<a href="javascript:openPopUP();">Click to open popup</a>

子窗口,即弹出窗口文件

下面的脚本应该在子窗口中。子窗口将打开,并在1秒后自动隐藏。在setTimeout方法中指定的值1000是打开子窗口的持续时间(以毫秒为单位)。如果希望子窗口打开的时间更长,可以增加此超时持续时间。

<body onLoad="setTimeout('window.blur()', 1000);" 
          onFocus="setTimeout('window.blur()', 1000);">

这可能对您有用:将这行代码添加到子窗口的onload事件中。。。

window.parent.opener.focus(); 
popunder = window.open('http://www.google.com','newwindow','width=200, height=200', "_blank");
popunder.blur();
window.focus();

试试这样的方法。

window.open(url); 
self.focus();

虽然它会导致安全问题,但我想其中一种方法是:

popup = window.open('http://www.google.com', 'newwindow',  "_blank");
popup.blur();

这只适用于IE。对于其他浏览器,您需要将安全级别设置为最低级别。

popunder = window.open('your_site_url','newwindow','width=500, height=500', "_blank");
popunder.blur();
window.focus();

或简称

window.open(url); self.focus();

如果你想要真正的popunder功能,有一个很棒的jQuery插件可以在https://github.com/hpbuniat/jquery-popunder

它使用各种技术的组合来跨各种浏览器实现它。并非所有浏览器都能处理相同的问题,但结果非常相似。

当前浏览器兼容性列为:(上次更新于2018年4月)

  • Mozilla Firefox 3-57
  • 谷歌Chrome 10-62
  • Microsoft Internet Explorer 6-11-MSIE 6-8需要jquery 1.x
  • Apple Safari 5

最后,它只适用于chrome浏览器。在父窗口中添加此代码:

<script type="text/javascript" language="javascript"> 
    function popWindow() 
    { 
    var popupo = window.open("popup.html", 'newwindow','toolbar=no,status=no,width=650,height=450', "_blank");
    popupo.moveTo(0, 0);
    popunder.blur();
    window.focus();
    } 
</script>

在子窗口主体加载事件中添加此代码作为

<body onLoad="setTimeout('window.blur()', 1000);" onFocus="setTimeout('window.blur()', 1000);"> 

没有新窗口,我们可以创建一个iframe并在iframe中打开您的url,然后使iframe显示none。如果您愿意,还可以打印iframe内容。

var ifrmId = 1;
jQuery(document).on('click', '.myFunc', function(e) {
    var url = 'www.xyz.com';
    var ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", url);
    ifrm.setAttribute("id", ifrmId);
    document.body.appendChild(ifrm);
    window.frames[ifrmId].focus();
    window.frames[ifrmId].contentWindow.print();
    document.getElementById(ifrmId).style.display = "none";
    ifrmId = ifrmId + 1;
});