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>
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!");
}
}
});
});