using modernizr to determine if multiple window open is supported

两盒软妹~` 提交于 2019-12-13 15:40:22

问题


the problem I've encountered is documented here.
window.open behaviour in chrome tabs/windows

where you can not open multiple windows via javascript in chrome.

I would like to open the multiple windows if it is supported, if it is not supported I will simply return a list of links.

is there a way using modernizr or something besides browser sniffing that I can determine if the behavior is supported?


回答1:


This ability to open multiple windows various widely between browser and even by browser config. So never assume you will be able to open multiple pop ups, you might be able to, but you can only know by testing, it's very easy to test tough.

To test if opening a pop up succeeded, inspect the return value.

var popupWindow = window.open('http://www.google.com/');
if (!popupWindow) {
    console.log('the window did not open');
    // do other stuff
} 

If the window opened the return value will be a Window object. If the the window did not open, the return value will be be falsy, this exact return value can vary from pop up blocker to pop up blocker, but generally you can assume the value to be falsy if the window did not open; meaning undefined or null.
As such it's very easy to trigger an alternate method in case the window failed to open.

You do not need modernizr or any plugins for this, this behavior of returning the Window object is the same in all browsers.

MDN reference:
https://developer.mozilla.org/en-US/docs/Web/API/Window/open

Firefox and Safari seem to support opening multiple windows by default. Chrome however will block the second window and show the little "pop up" blocked message. Additionally Chrome will also block opening windows that did not originate from direct users actions; meaning a click or a key press.




回答2:


Nothing like modernizr or any custom code is going to give you any type of feature detection. The main reason is because all major browsers require some sort of user action to open a new window programmatically - usually a click. So creating a feature detection is out of the question.

This is an interesting question and one where thinking in terms of "progressive enhancement" might help you get to a good solution.

First, let's assume that you cannot open multiple windows in any browser. What would you do? Show a list of links as you've suggested. By adding something like target="_blank" to each link, now we have a working app without any JavaScript (or if the user has JavaScript disabled):

<section id="links-wrap">
  <a href="/page-A.html" target="_blank" />
  <a href="/page-B.html" target="_blank" />
</section>

This baseline of functionality will work on every single browser ever made - your Treo visitors will love you. However, this experience is less than ideal because the links are likely to open new tabs instead of new windows. So let's use JavaScript to open a new window whenever a link is clicked. Lets also hide each link after it is clicked and position each window so that they are not overlapping:

function openWindowFromLink (link, idx) {
  var top = idx % 2 * 600;
  var left = Math.floor(idx/2) * 600;
  var win = window.open(link.href, 'Window '+ top +'x'+ left, 'width=600,height=600,top='+ top +',left='+ left);
  if (win) {
    link.style.display = "none";
  }
  return win;
}

function handleLinkClick(ev) {
  ev.preventDefault();
  var link = ev.target;
  var idx = 0;
  var prev = link.previousSibling;
  while (prev) {
    if (prev.nodeType === 1) {
        idx++;
    }
    prev = prev.previousSibling;
  }
  openWindowFromLink(link, idx);
}

document.getElementById('links-wrap').addEventListener('click', handleLinkClick);

Now comes the hard part: how can we open many windows at once. As we know, Chrome will only allow one window to open per user click. While other browsers might not have this same restriction, they may add it in the future (I'm actually surprised that they don't all have this restriction right now). So lets assume that all browsers have the same limitation as Chrome. Users don't want to click every single link every time - so lets give them a click target that they can click really fast to open all of the windows. Creative wording will reduce the annoyance of this task.

<div id="rapid-click-box">
  Click me really fast and see what happens!
</div>

... and some JavaScript:

var clickBox = document.getElementById('rapid-click-box');
var clickCount = 0;
clickBox.addEventListener('click', function handleRapidClick (ev) {
    var link = links[clickCount];
    if (link.style.display !== 'none') {
        openWindowFromLink(link, clickCount);
    }

    if (++clickCount === links.length) {
      clickBox.removeEventListener('click', handleRapidClick);
      clickBox.style.display = 'none';
    }
});

Finally, lets take care of those browser which allow multiple windows to be opened at once. We still need the user to click in order to call window.open - so lets get creative and see how we can make the user click something. A cleverly worded welcome message should suffice:

<div id="welcome-message" style="display:none">
  <h1>Hey, welcome to my site. Are you a human?</h1>
  <button>Yes</button>
</div>

<script>
// show the welcome message immediately if JS is enabled
document.getElementById('welcome-message').style.display = 'block';
</script>

... and once again, a little bit of JavaScript:

var button = document.getElementsByTagName('button')[0];
button.addEventListener('click', function handleYesClick (ev) {
    ev.preventDefault();
    button.removeEventListener('click', handleYesClick);
    document.getElementById('welcome-message').style.display = 'none';

    for (var i = 0, l = links.length; i < l; i++) {
      if ( !openWindowFromLink(links[i], i) ) {
        break;
      }
    }

    if (i === links.length) {
      clickBox.style.display = 'none';
    }
});

And a fiddle to show it all in action:

https://jsfiddle.net/q8x5pqsw/



来源:https://stackoverflow.com/questions/37685904/using-modernizr-to-determine-if-multiple-window-open-is-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!