Pop up blocker API- how to check if user has it enabled

后端 未结 5 1001
野趣味
野趣味 2021-02-04 00:37

I need to know when the user clicks on the button that triggers window.open if there is stable API/way to know beforehand if the user actively has

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 00:53

    window.open() will mostly only be blocked if it was not triggered by a click event.

    To confirm the window has loaded:

    var loaded = false;
    
    function windowLoaded() { 
      alert("The popup loaded"); 
      loaded = true
    }
    
    function pause(milliseconds) {
      var dt = new Date();
      while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
    }
    
    document.write("start...");
    
    //open the window
    var win = window.open("window.html");
    
    // If window.open returned an object
    if(win) {
      win.onload = function() { 
        win.RunCallbackFunction = windowLoaded; 
      };
      document.write("popup sent...");
    
      pause(3000);
    
      // Verify that out window loaded
      if (loaded == false)
        document.write("check you popup blocker!");
      else
        document.write("ok!");
    
    }
    else {
      document.write("window.open() was blocked...");
    }

提交回复
热议问题