7

There is a way in JS to open window and close it immediately, that it will not open in new tab , I just want want to open a window (like in background if possible ) do some check on it and immediately close it, without the effect that the new tab is open , is it possible?

Something like this but without the effect that the window is opened...

 var popup = window.open("http://www.google.com");

popup.close();

UPDATE: Here is solution which I found but it's flicking sometimes and I want to avoid it...

function goNewWin() {

    var TheNewWin = window.open("http://www.yahoo.com", 'TheNewpop', 'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');

    TheNewWin.blur();
    TheNewWin.close();

}
  • use an iframe instead – Jaromanda X Oct 10 '16 at 07:32
  • @JaromandaX - I need the window since I need to check for pop-up blocker if is enabled ...,is it possible with iframe? –  Oct 10 '16 at 07:41
  • oh, so you want to open a popup, - the "new tab" thing in your question was confusing ... add some [position and size features](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Position_and_size_features) to the `window.open` to get a popup – Jaromanda X Oct 10 '16 at 07:43
  • @JaromandaX - not sure that I got it size and position to open and close like background mode ?, can you please provide full example ? Thank you! –  Oct 10 '16 at 08:08
  • see [this tutorial](http://accessify.com/features/tutorials/the-perfect-popup/) – Jaromanda X Oct 10 '16 at 08:13
  • @JaromandaX - Thanks but this not helps for the issue of open and close window like in bg mode..., any other idea? –  Oct 13 '16 at 10:17
  • @RaynD so what you need is only to check if the popUp blocker is enabled? If yes, you can try [this solution](http://stackoverflow.com/questions/2914/how-can-i-detect-if-a-browser-is-blocking-a-popup) or look after others in stackoverflow – Jacobi Oct 13 '16 at 20:19
  • 3
    For security reasons, the popup window created by `window.open` must have a minimum size and must be entirely visible on the screen (except if actually moved by the user). Therefore, you will at least see a small window appear briefly before it closes. Documentation: for [Internet Explorer](https://msdn.microsoft.com/en-us/library/ms531202(v=VS.85).aspx), for [Firefox](https://developer.mozilla.org/en-US/docs/Web/API/Window/open). – ConnorsFan Oct 14 '16 at 19:52
  • 1
    But why you need a 'window' for that, you could make use of any of ajax methods. Pop up window opening and closing always will flick.. and if the user is using any popup blocker, it will not work. – sarath Oct 19 '16 at 12:26
  • What do you need the popup to do? Can you use a hidden iframe (another windowed element) – Ron Gilchrist Oct 19 '16 at 22:42

10 Answers10

3

Even if you call close immediately after window.open, and even if you try to locate the popup window outside of the screen or to make its size null, you will see it quickly flash on the screen:

var popup = window.open("", "Popup", "left=10000,top=10000,width=0,height=0");
popup.close();

For security reasons, window.open cannot position the popup window outside of the screen and cannot make it smaller than some minimal size. Here is an excerpt of the window.open topic in the MDN documentation:

Requested position and requested dimension values in the features list will not be honored and will be corrected if any of such requested value does not allow the entire browser window to be rendered within the work area for applications of the user's operating system. No part of the new window can be initially positioned offscreen. This is by default in all Mozilla-based browser releases.

Similar restrictions apply in Internet Explorer, as indicated in this document. The minimum size is 250 × 150 in Internet Explorer, and the minimum "inner" size is 100 × 100 in Firefox.

If you absolutely want to avoid the flashing popup window, you can use one of these methods:

  • Instead of testing with a call to window.open, display a warning on the page to inform the user that the popup blocker option should not be activated (as suggested here by UncaAlby)
  • Let the application run and warn the user, after the fact, if the real popup window could not be displayed
  • Use an alternative to window.open, for example the dialog widget from jQuery UI or the modals from Bootstrap
Community
  • 1
  • 1
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
2

A solution to your problem is by using the window.postMessage() function. The child is used to notify the parent that the window has been loaded and it also works on cross domain.

Child

$(window).load(function(){
    this.opener.postMessage({'loaded': true}, "*");
    this.close();
});

Parent

$(window).on('message', function(event) {     
    alert(event.originalEvent.data.loaded);
}); 

There is also one more method as described in this thread of the stackoverflow.

Full attribution is given to the author of the stack overflow of this thread

Community
  • 1
  • 1
Pritish Vaidya
  • 21,561
  • 3
  • 58
  • 76
1
<html>
<head>
<script type= "text/javascript">




function closeWin() {
    window.close();
    return true;
}
</script>

</head>
<body onload="return closeWin();">

</body>
</html>
0

// Code for page from where you want to open the popup it will open small popup in right bottom corner.

var popup = window.open("popup.html", "Popup", "height=1, width=1, status=no, toolbar=no, menubar=no, location=no, top = 100000, left=100000 ");

// In your popup.html add follwing code

this.close();
Mitul
  • 3,431
  • 2
  • 22
  • 35
  • I try it out and its still flicking , any idea how to avoid it that user will not see this effect ?Thank you!!! –  Oct 21 '16 at 14:07
0

How about dynamicaly create an iframe on the current window. Then you can open any page on it, later you could destroy iframe element at any time.

CroMagnon
  • 1,218
  • 7
  • 20
  • 32
0
<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    window.open("http://www.google.com");
}
</script>

here you go the answer that it will open the page new tab on button click

Mayur Aglawe
  • 481
  • 3
  • 3
0

If you don't want user to know that you did some process on some url use AJAX call and process

or you do this

<script type="text/javascript">
var popup = window.open("http://www.exampleurl.com/popup.html", "Popup", "height=10, width=10, status=no, toolbar=no, menubar=no, location=no, top = 1, left=1");
</script>

here in popup.html you can write code to close it.

Jazzzzzz
  • 1,593
  • 15
  • 16
0

This open window in a hidden mode. not visible to user. Hope this helps you

window.open(path.html,'_blank','toolbar=no,status=no,menubar=no,scrollbars
=no,resizable=no,left=10000,     top=10000, width=10,    height=10,visible=none', '');
  • Thanks but it still flicking , any idea how to avoid this flicking issue? –  Oct 21 '16 at 14:06
0

I don't think you can avoid the flicker of opening a new window completely. You could check for a pop-up and save the state in a cookie. Then wrap your pop-up checker in a simple if statement with the value of that cookie as the logic. This would avoid the pop-up checker going every single page load.

 var popUpCookie = "getCookie";
 if(!popUpCookie){
      var newWin = window.open("http://www.example.com");             

      if(!newWin || newWin.closed || typeof newWin.closed=='undefined') 
      { 
          //POPUP BLOCKED - Create Cookie
      }else{
          //POPUP NOT BLOCKED - Create Cookie
      }
 }
Travis Peterson
  • 373
  • 3
  • 15
0

Code i have checked and its working

var popup =  window.open("test.php","event","width=900,height=900,scrollbars=no,resizable=yes");
popup.blur();
window.focus();

This will open the page but you can't see the page open.

sowmya
  • 221
  • 1
  • 3
  • 15