javascript - Cannot catch window.open() exception in Safari - Stack Overflow

admin2025-04-20  0

I have some code that opens a link. The link is configurable and thus I am doing some basic error handling. Specifically I am wrapping the window.open() call with a try/catch since the built in API will throw an exception if the URL is invalid. The issue is that in Safari the catch doesn't get hit.

I've tried looking through the Safari API but I cannot find any good information.

The below example works just fine in IE and Chrome but NOT in Safari.

$(function() {
  $('button').on("click", function() {
    try {
      var begin = "http://<";
      var opentag = "script>";
      var stuff = "function(){alert('unsafe');}";
      var all = begin + opentag + stuff;
      window.open(all);
    } catch (e) {
      alert("errr");
    }
  });
});
<script src=".1.1/jquery.min.js"></script>
<button>
Click ME
</button>

I have some code that opens a link. The link is configurable and thus I am doing some basic error handling. Specifically I am wrapping the window.open() call with a try/catch since the built in API will throw an exception if the URL is invalid. The issue is that in Safari the catch doesn't get hit.

I've tried looking through the Safari API but I cannot find any good information.

The below example works just fine in IE and Chrome but NOT in Safari.

$(function() {
  $('button').on("click", function() {
    try {
      var begin = "http://<";
      var opentag = "script>";
      var stuff = "function(){alert('unsafe');}";
      var all = begin + opentag + stuff;
      window.open(all);
    } catch (e) {
      alert("errr");
    }
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
Click ME
</button>

Share Improve this question asked Sep 18, 2017 at 19:24 alaneyalaney 6235 silver badges12 bronze badges 3
  • 1 Each window operates in its own Javascript thread that runs asynchronously with other windows. – Barmar Commented Sep 18, 2017 at 19:47
  • This is not a problem of code executing in another window. This is, the URL to be opened may be invalid and if it is I want to catch the exception. – alaney Commented Sep 18, 2017 at 21:04
  • I am not sure about Safari, but Chrome does not throw any exception with popup blocker on. The open simply returns a null value. The open use to be sync. If somehow you still cannot catch a thrown exception, then you can workaround it by adding a window.onerror listener. – inf3rno Commented Jan 9, 2019 at 21:32
Add a ment  | 

1 Answer 1

Reset to default 2

Barmar's ment is right I think. A way around it would be to grab the return value of window.open() and check to see if it returned anything. If not, then it probably didn't open the window. Read the window.open() documentation here: https://developer.mozilla/en-US/docs/Web/API/Window/open

$(function() {
  $('button').on("click", function() {
    var x = null;
    try {
      var begin = "http://<";
      var opentag = "script>";
      var stuff = "function(){alert('unsafe');}";
      var all = begin + opentag + stuff;

      x = window.open(all);
    } catch (e) {
      alert("errr");
    } finally {
      if (!x) {
        alert("errrrrrrrrr!");
      }
    }
  });
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123134a286267.html

最新回复(0)