0

On blur I am trying to get a new tab to open only after user confirms to do this. However, I cannot see this happening and no confirm dialog is triggered. I am using the below and have tried other scripts too but cannot find how to do this:

 $(window).blur(function() {
      confirmit() { 
      var closeit= confirm("Would you like to see our exclusive offers?"); 
      if (closeit == true) {
        window.open("http://NEWURLHERE.com" target="_blank");
      }
      else {
        window.close();
      }
    };
})
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Manny
  • 59
  • 8

4 Answers4

0

first define confirmit and for that, add the keyword function before confirmit.
Then after closing }, call it by confirmit();
Also, you can't pass arguments simply to a window.open function like you use in <a tag by seperating using spaces. Just put a comma(,) to seperate arguments.
Remove target= and just pass the value.
However it will not work in IE. Check here to see Why? Chrome, Javascript, window.open in new tab

Check the below code. Run it, click on the output screen and then click outside

$(window).blur(function() {
   function confirmit() { 
      var closeit= confirm("Would you like to see our exclusive offers?"); 
      if (closeit == true) {
        window.open("http://NEWURLHERE.com",'_blank');
      }
      else {
        window.close();
      }
    };
    confirmit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Community
  • 1
  • 1
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • thanks for all the answers. Most work but the new tab gets blocked by popup blocker. As user confirms to open why does it still block? I don't want it to block is there any way around this – Manny Mar 05 '17 at 16:37
  • It is either a browser security feature or done by a pop up blocker. Glad it helped you mate. Consider marking it as answer for future visitors. – Sagar V Mar 05 '17 at 16:43
0

Confirm if you've included the jQuery library in your code. I was able to reproduce exactly whatever you stated.

$(window).blur(function() {
  var closeit = confirm("Would you like to see our exclusive offers?");
  if (closeit == true) {
    window.open("http://NEWURLHERE.com",
      target = "_blank");
  } else {
    window.close();
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Himanshu
  • 490
  • 1
  • 8
  • 17
0

This will help.

var confirmit = function(){ 
      var closeit= confirm("Would you like to see our exclusive offers?"); 
      if (closeit == true) {
        alert("You chose Ok");
        // window.open("http://NEWURLHERE.com" target="_blank");
      }
      else {
        alert("You chose Cancel");
        // window.close();
      }
    }
 
 $(window).blur(function() {
    confirmit();  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
0

$(window).blur(function() {
      var closeit= confirm("Would you like to see our exclusive offers?"); 
      if (closeit == true) {
        window.open("http://NEWURLHERE.com", target="_blank");
      }
      else {
        window.close();
      }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

behkod
  • 2,647
  • 2
  • 18
  • 33