Cannot catch window.open() exception in Safari

Viewed 6818

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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
Click ME
</button>

1 Answers
Related