macos - Safari not catching exception when trying to access parent window object with Javascript trycatch - Stack Overflow
- c - Solaris 10 make Error code 1 Fatal Error when trying to build python 2.7.16 - Stack Overflow 推荐度:
- javascript - How to dismiss a phonegap notification programmatically - Stack Overflow 推荐度:
- javascript - Get the JSON objects that are not present in another array - Stack Overflow 推荐度:
- javascript - VS 2015 Angular 2 import modules cannot be resolved - Stack Overflow 推荐度:
- javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 推荐度:
- 相关推荐
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/
- 这个夏天不平静 苹果WWDC 2013抢先看
- powershell - Windows AutoPilot - Set Auto Login - Stack Overflow
- apache beam - Unable to Write processed Data to Parquet - Stack Overflow
- typescript - Npm 404 Not found - Stack Overflow
- node.js - Parsing error when sending data from godot to api - Stack Overflow
- python - Plotting a list of integer 3d values into Axes3d.voxels - Stack Overflow
- typescript - Command 'pod install' failed, Cause: binbash -c - Stack Overflow
- d3.js - d3 - graph is rendered outside the svg - Stack Overflow
- node.js - Test backend functionality nodespostman error - Stack Overflow
- typescript - Declare object and internally refer to other values - Stack Overflow
- differential equations - Calculating a rocket trajectory in Matlab - Stack Overflow
- reactjs - How to import svg icons in Nextjs 15 - Stack Overflow
- python - Pyinstaller (Mac App) - Why only permission given to the executable file (instead of .app bundle) could work? - Stack O
- utf 8 - correct way to Import database dump in PowerShell - Stack Overflow
- postgresql - Custom stopwords dictionary in Postgres hosted on GCP Cloud SQL - Stack Overflow
- applescript - How do I interact with the macOS share sheet? - Stack Overflow
- python - Reading .msg attachments from a .msg file - Stack Overflow