So the code below can be seen working via the fiddle link. Safari is refusing to catch the exception- I am assuming this may be because it is not a 'Javascript' error? Either way, if you run the code in any other browser, you will see the page URL in the console.
The purpose of the function is find the page URL when executed multiple iframes deep on the page. If anyone can confirm why Safari won't catch the error and/or maybe offer a solution, that would be great...Thanks!
function logURL() {
var oFrame = window,
exception = false;
try {
while (oFrame.parent.document !== oFrame.document) {
oFrame = oFrame.parent;
}
} catch (e) {
exception = true;
}
if(exception) {
console.log('excepted', oFrame.document.referrer);
} else {
console.log('no exception', oFrame.location.href);
}
}
http://jsfiddle.net/HPu9n/82/
The reason why your try catch block is failing is because an ajax request is asynchronous. The try catch block will execute before the Ajax call and send the request itself, but the error is thrown when the result is returned, AT A LATER POINT IN TIME. When the try catch block is executed, there is no error.
You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions. See the JavaScript Guide for more information on JavaScript exceptions.
The catch statement lets you handle the error if any are present. The throw statement lets you make your own errors. The finally statement lets you execute code, after try and catch. The finally block runs regardless of the result of the try-catch block.
The try-catch statement should be used any time you want to hide errors from the user, or any time you want to produce custom errors for your users' benefit. If you haven't figured it out yet, when you execute a try-catch statement, the browser's usual error handling mechanism will be disabled.
While an error is logged to the Safari console, Safari apparently does not thrown a JavaScript exception when accessing a window property across frame origins. Instead, Safari simply returns undefined
for any property which is not accessible cross-origin.
With this knowledge, we can simply check if oFrame.parent.document
is set, and if it's not, break off the loop an do what would happen if the browser threw an exception.
function logURL() {
var oFrame = window,
exception = false;
try {
while (oFrame.parent.document !== oFrame.document) {
//Check if document property is accessible.
if (oFrame.parent.document) {
oFrame = oFrame.parent;
}
else {
//If document was not set, break the loop and set exception flag.
exception = true;
break;
}
}
} catch (e) {
exception = true;
}
if(exception) {
console.log('excepted', oFrame.document.referrer);
} else {
console.log('no exception', oFrame.location.href);
}
}
logURL();
excepted http://jsfiddle.net/ggh0a0f4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With