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 (top.functionName, top.variable). What I am looking for is a way to set variables and call functions from the highest frame that still remains on the domain. For example, top frame might be externaldomain.com, frame within that might be ourdomain.com and frame within that might be ourdomain.com/somepage and so on. How would we loop through the frames to get the closest to the top that is still on the same domain?
Thanks,
The following function will return the closest frame. An error will be thrown if the frame is not at the same origin, because the
documentproperty 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/
The
whileloop will only break if:top.document !== documentwill evaluate tofalse