Get the highest frame that is still in the domain

I have a session function that works well in one domain, but when it is used through our api on another site, it does not work, because we install and call from the top frame (top.functionName, top.variable). I am looking for a way to set call variables and functions from the highest frame that still remains in the domain. For example, the top frame can be externaldomain.com, the frame inside which can be ourdomain.com and the frame inside this can be ourdomain.com/somepage, and so on. How will we scroll frames to get the one closest to the top, which is still in the same domain?

Thank,

0
javascript
Dec 29 '11 at 19:51
source share
1 answer

The next function will return the closest frame. An error will be selected if the frame does not have the same origin, because the document property cannot be read if the reader has a different origin.

Fiddle: http://jsfiddle.net/txmdL/3/
JSfiddle runs on two domains: fiddle.jshell.net and jsfiddle.net. I included some fiddle.jshell.net scripts in the demo. If the script works as expected, clicking on the button will display a dialog containing http://fiddle.jshell.net/txmdL/3/show/

 function getClosestTop(){ var frame = window; try { while (frame.parent.document !== frame.document) frame = frame.parent; } catch(e){} return frame; } 

The while will only break if:

  • Reached the top. top.document !== document will evaluate to false
  • The parent window does not have the same origin. The error will be reset.
+4
Dec 29 '11 at 19:56
source share



All Articles