admin管理员组文章数量:1435859
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
1 Answer
Reset to default 6 +50While 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/
本文标签:
版权声明:本文标题:macos - Safari not catching exception when trying to access parent window object with Javascript trycatch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673714a2669723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论