2012-07-10 53 views
12

我想在點擊時打開一個窗口,但是我想在當前窗口後面打開它,或者當新窗口打開時它應該最小化本身。我做了一個功能,但它實際上沒有工作。使用Javascript/jQuery在當前窗口後面打開一個窗口

<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> 
+3

聽起來像危險的那些點擊,煩人,彈出式廣告... – dda 2012-07-10 05:36:39

+2

谷歌「背後彈出式窗口。 「基本上你是window.open,接着是window.focus。 – 2012-07-10 05:47:08

+0

GGG:它不工作 – Carlos 2012-07-10 09:26:14

回答

18

編輯

父窗口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(); 
+0

@amit工作 - 檢查我的編輯回答這個migh thwlp你實現你想要.. – 2012-07-16 09:20:46

+1

不,它不工作。彈出開放,它仍然盈父窗口 – Carlos 2012-07-17 04:27:55

+0

@amit的 - 它的彈出窗口,在1秒隱藏正常的自然那它是如何工作?可能沒有其他辦法可以做到這一點.. – 2012-07-17 05:38:19

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

我已經嘗試這一點,但它不工作 – Carlos 2012-07-14 13:00:22

+0

不知道瀏覽器已經識破了這種技術,但是這並不在任何FF或Chrome,這些天的工作。 Popup停留在前面。 – Brade 2014-03-25 23:00:56

6

嘗試這樣的事情。

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

不工作:(。 – poitroae 2017-11-04 20:18:44

5

雖然它會導致安全問題,但我想的做的事情是這樣的一個:

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

這將在IE才能正常工作。對於其他瀏覽器,您需要將安全級別設置爲最低。

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

或者乾脆

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

如果你想真正的隱藏彈出式的能力有很大的jQuery插件在https://github.com/hpbuniat/jquery-popunder

處理呢它採用的各種技術的組合,以在各種瀏覽器中完成它。並非所有瀏覽器都可以處理它,但結果非常相似。

當前瀏覽器的兼容性被列爲

  • Mozilla Firefox瀏覽器3-30
  • 谷歌瀏覽器10-35
  • 的Microsoft Internet Explorer 6-11 - MSIE 6-8需要的jQuery 1.x的
  • Apple Safari 5
1

最後,它只在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> 

添加在子窗體onload事件,該代碼

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