13

I want to open a window on click, but I want to open it behind the current window, or when the new window opens it should minimize itself. I have made a function but it actually did not work.

<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>
Ravindra S
  • 6,302
  • 12
  • 70
  • 108
Jitender
  • 7,593
  • 30
  • 104
  • 210

8 Answers8

20

EDIT

Parent window 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>

Child Window i.e PopUp Window File

The below script should be in the child window. The child window will open and will automatically be hidden after 1 second. The value 1000 specified in the setTimeout method is the duration in milliseconds for which the child window will be open. You can increase this timeout duration if you want the child window to be open longer.

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

This might work for you: Add this line of code in the onload event of your child window...

window.parent.opener.focus(); 
Sarath Chandra
  • 1,850
  • 19
  • 40
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
13
popunder = window.open('http://www.google.com','newwindow','width=200, height=200', "_blank");
popunder.blur();
window.focus();
odupont
  • 1,946
  • 13
  • 16
  • 1
    Not sure if browsers have wised up to this technique, but this does NOT work in either FF or Chrome these days. Popup stays in front. – Brade Mar 25 '14 at 23:00
6

Try something like this.

window.open(url); 
self.focus();
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Rorchackh
  • 2,113
  • 5
  • 22
  • 38
5

Although it causes security issues, but I guess one of the way to do it is :

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

This will work only in IE. For other browsers you need to set the security level to minimum.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
NG.
  • 5,695
  • 2
  • 19
  • 30
3
popunder = window.open('your_site_url','newwindow','width=500, height=500', "_blank");
popunder.blur();
window.focus();

or simply

window.open(url); self.focus();
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
Hiren Soni
  • 574
  • 3
  • 11
2

If you want real popunder capabilities there is a great jQuery plugin to handle it at https://github.com/hpbuniat/jquery-popunder

It uses a combination of various techniques to accomplish it across various browsers. Not all browsers can handle it the same but the results are very similar.

Current browser compatibility is listed as: (last updated April '18)

  • Mozilla Firefox 3-57
  • Google Chrome 10-62
  • Microsoft Internet Explorer 6-11 -- MSIE 6-8 requires jquery 1.x
  • Apple Safari 5
Al.G.
  • 4,327
  • 6
  • 31
  • 56
Mike Grace
  • 16,636
  • 8
  • 59
  • 79
1

Finally , it was working for me only in chrome browser. Add this code in Parent window:

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

Add this code in child window body onload event as

<body onLoad="setTimeout('window.blur()', 1000);" onFocus="setTimeout('window.blur()', 1000);"> 
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
0

Insted of a new window, we can create an iframe and open your url in the iframe and then make the iframe display none also print the iframe content if you want.

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;

}); 
Elikill58
  • 4,050
  • 24
  • 23
  • 45