Friday, June 25, 2010

Accessing elements cross-frame (HTML)

I know it is a hassle, but sometimes you need to maintain legacy code and come across someone using frames. To access elements in other frames you need to go to the top and 'back down into the frame you want':

<name of parent frame>.<name of sibling frame>.element
parent.mainframe.location.href = 'next.html'

Remember javascript variables that should be accessible cross-frame need to be global (not declared with var (var variablename)). Also for jQuery you use the document as a root to get elements from. e.g.:
parent.mainframe.document.myVar = 5;

Check out these pages for more information: http://forum.jquery.com/topic/jquery-getting-to-a-variable-in-the-parent-frame-stumped, http://anothertechnicalblog.blogspot.com/2009/08/seleccion-de-elementos-dentro-de-un.html