Open window and close immediately

In JS, there is a way to open a window and close it immediately so that it does not open in a new tab , I just want to open a window (for example, in the background, if possible) do some checking and close it immediately, without the effect of opening a new tab , maybe is it?

Something like this, but without opening the window ...

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

UPDATE: Here is a solution that I found, but it sometimes changes, 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(); } 
+4
source share
9 answers

Your problem is window.postMessage() using the window.postMessage() function. child used to notify parent that the window has been loaded, and 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 another method described in this stackoverflow thread.

Full attribution is given to the author of this thread.

+1
source

Even if you call close immediately after window.open , and even if you try to find the pop-up window outside the screen or make it zero size, you will see that it quickly blinks 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 pop-up outside the screen and cannot make it smaller than the minimum. Here is a fragment of window.open in the MDN documentation :

The requested position and the requested measurement values โ€‹โ€‹in the list of functions will not be respected and will be corrected if any of the requested values โ€‹โ€‹does not allow the entire browser window to be displayed within the workspace for user operating system applications. No part of a new window can be originally placed off-screen. This is the default in all versions of Mozilla-based browsers.

Similar restrictions apply in Internet Explorer, as indicated in this document . The minimum size is 250 times 150 in Internet Explorer , and the minimum "internal" size is 100 ร— 100 in Firefox .

If you absolutely want to avoid the popup popup, you can use one of the following methods:

  • Instead of testing with window.open display a warning on the page to tell the user that the pop-up blocker should not be activated (as suggested here by UncaAlby)
  • Let the application start and warn the user after the fact if the real popup cannot be displayed
  • Use an alternative to window.open , for example, a dialog widget from the jQuery user interface or modals from Bootstrap
+1
source
 <html> <head> <script type= "text/javascript"> function closeWin() { window.close(); return true; } </script> </head> <body onload="return closeWin();"> </body> </html> 
+1
source

// The code for the page from which you want to open the popup window will open a small popup window in the lower right 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(); 
0
source

How about dynamically creating an iframe in the current window. Then you can open any page on it, later you can destroy the iframe element at any time.

0
source
 <button onclick="myFunction()">Try it</button> <script> function myFunction() { window.open("http://www.google.com"); } </script> 

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

0
source

If you donโ€™t want the user to know that you have done some process at some URL, use the AJAX call and the process

or you do it

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

0
source

This is an open window in stealth 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', ''); 
0
source

I do not think that you can avoid the flickering of opening a new window. You can check the popup and save the state in a cookie. Then wrap your popup check in a simple if statement with the value of this cookie as logic. This avoids the popup checking that loads each page.

  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 } } 
0
source

Source: https://habr.com/ru/post/1257952/


All Articles