最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open window and close immediately - Stack Overflow

programmeradmin3浏览0评论

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

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("", 'TheNewpop', 'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');

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

}

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

}
Share Improve this question edited Oct 13, 2016 at 10:16 asked Oct 10, 2016 at 7:30 user6124024user6124024 10
  • use an iframe instead – Jaromanda X Commented Oct 10, 2016 at 7:32
  • @JaromandaX - I need the window since I need to check for pop-up blocker if is enabled ...,is it possible with iframe? – user6124024 Commented Oct 10, 2016 at 7:41
  • oh, so you want to open a popup, - the "new tab" thing in your question was confusing ... add some position and size features to the window.open to get a popup – Jaromanda X Commented Oct 10, 2016 at 7:43
  • 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, for Firefox. – Martin Parenteau Commented Oct 14, 2016 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 Commented Oct 19, 2016 at 12:26
 |  Show 5 more comments

10 Answers 10

Reset to default 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

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

<html>
<head>
<script type= "text/javascript">




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

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

</body>
</html>

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.

<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

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

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.

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', '');

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

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.

发布评论

评论列表(0)

  1. 暂无评论