Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari not catching exception when trying to access parent window object with Javascript try/catch

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/

like image 208
Oli C Avatar asked Jan 30 '15 18:01

Oli C


People also ask

Why is my try catch not working JavaScript?

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.

Can we use nest try catch in JavaScript?

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.

Can you throw error in catch block JavaScript?

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.

Should I use try catch everywhere JavaScript?

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.


1 Answers

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.

Example (JSFiddle):

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();

Logs:

excepted http://jsfiddle.net/ggh0a0f4/
like image 188
Alexander O'Mara Avatar answered Oct 20 '22 21:10

Alexander O'Mara