Skip to content Skip to sidebar Skip to footer

Get Highest Frame That Still Is On Domain

I have a session function that works well on the same domain, but when it is used through our api on another site, it doesn't work because we are setting and calling from top frame

Solution 1:

The following function will return the closest frame. An error will be thrown if the frame is not at the same origin, because the document property cannot be read if the reader is from a different origin.

Fiddle: http://jsfiddle.net/txmdL/3/ JSfiddle runs on two domains: fiddle.jshell.net and jsfiddle.net. I have embedded multiple fiddle.jshell.net fiddles in the demo. If the script works as expected, clicking at the button will show a dialog containing http://fiddle.jshell.net/txmdL/3/show/

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

The while loop will only break if:

  • The top has reached. top.document !== document will evaluate to false
  • The parent window is not at the same origin. An error will be thrown.

Post a Comment for "Get Highest Frame That Still Is On Domain"