macos - Safari not catching exception when trying to access parent window object with Javascript trycatch - Stack Overflow

So the code below can be seen working via the fiddle link. Safari is refusing to catch the exception- I

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

/

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/HPu9n/82/

Share Improve this question edited Feb 2, 2015 at 23:17 Alexander O'Mara 60.7k19 gold badges173 silver badges181 bronze badges asked Jan 30, 2015 at 18:02 Oli COli C 1,17814 silver badges38 bronze badges 7
  • In my puter safari console is showing the url. no exception stackoverflow./questions/28241940/… – Sandro Eric Commented Feb 2, 2015 at 14:39
  • Thanks Sandro- This is a problem on Safari running on OSX, are you on OSX as well? – Oli C Commented Feb 2, 2015 at 14:56
  • 1 I can reproduce this in Safari 7.1.3 on OS X. – Alexander O'Mara Commented Feb 2, 2015 at 15:09
  • 1 I am Safari Version 7.0.3 and OS X Version 10.9.2 – Sandro Eric Commented Feb 2, 2015 at 17:06
  • 1 Still an issue in Safari 9.1.2. Getting the feeling they aren't going to fix this. – Mordred Commented Oct 21, 2016 at 1:12
 |  Show 2 more ments

1 Answer 1

Reset to default 6 +50

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/ggh0a0f4/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745673715a4639558.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信