I would suggest that each page searches for the desired value by calling a method. Basically exposing the search interface. Then you only need to call the method on the landing page from the parent page. Proper naming will help developers understand what is happening, and using methods will simplify the logic.
Or, if you only need to get the value from the parent page, you can register the hook with each page in an iframe using a common interface. Each page can simply call this hook to get the value. This prevents the complex logic of determining page level. Sort of
iframe1.GetValueHook = this.GetValue; iframe2.GetValueHook = this.GetValue;
Then each page can simply call
var x = this.GetValueHook();
If you have subpages, you can make it recursive. If you need a link between all pages, use the same approach, but with the registration process. Each page registers itself (and these are children) with its parent. But if you need to do this, you must reevaluate your architecture.
Example: register.js
var __FRAMENAME = "Frame1"; var __FIELDID = "fieldId"; var __frames = []; function RegisterFrame(frame) { __frames.push(frame); for (var i = 0; i < frame.children.length; i++) { __frames.push(frame.children[i]); } RegisterWithParent(); } function RegisterWithParent() { var reg = { name: __FRAMENAME, getvalue: GetFieldValue, children: __frames }; if(parent != undefined && parent != this) { parent.RegisterFrame(reg); } } function SetupFrame(name, fieldId) { __FRAMENAME = name; __FIELDID = fieldId; RegisterWithParent(); } function GetFieldValue() { return document.getElementById(__FIELDID).value; } function GetValueFrom(name) { for (var i = 0; i < __frames.length; i++) { if (__frames[i].name == name) { return __frames[i].getvalue(); } } }
index.html
<html> <head> <script language="javascript" type="text/javascript" src="register.js"></script> </head> <body> PAGE <input type="hidden" id="hid123" value="123" /> <iframe id="frame1" src="frame1.html"></iframe> <iframe id="frame2" src="frame2.html"></iframe> <script type="text/javascript"> SetupFrame("Index", "hid123"); setTimeout(function () { </script> </body></html>
frame1.html
<html> <head> <script language="javascript" type="text/javascript" src="register.js"></script> </head> <body> <input type="hidden" id="hid" value="eterert" /> <script type="text/javascript"> SetupFrame("frame1", "hid"); </script> </body></html>
frame2.html
<html> <head> <script language="javascript" type="text/javascript" src="register.js"></script> </head> <body> <input type="hidden" id="hid456" value="sdfsdf" /> <iframe id="frame2" src="frame3.html"></iframe> <script type="text/javascript"> SetupFrame("frame2", "hid456"); </script> </body></html>
frame3.html
<html> <head> <script language="javascript" type="text/javascript" src="register.js"></script> </head> <body> <input type="hidden" id="hid999" value="bnmbnmbnm" /> <script type="text/javascript"> SetupFrame("frame3", "hid999"); </script> </body></html>
It would be better if you could change it to use a dictionary / hash table instead of loops.