Thursday, October 20, 2016

Open window and close immediately

Leave a Comment

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();  } 

7 Answers

Answers 1

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

Answers 2

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

Answers 3

// 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(); 

Answers 4

<html> <head> <script type= "text/javascript">     function closeWin() {     window.close();     return true; } </script>  </head> <body onload="return closeWin();">  </body> </html> 

Answers 5

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.

Answers 6

<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

Answers 7

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment