- 判断当前页面是否是iframe的子页面的方法:
可以通过判断页面的parent.location和self.location是否相等。
if(parent&&parent.location==self.location){ //is not a iframe page }else{ //this is a iframe page }
- 在子页面中调用父页面所定义的函数:
假设父页面parent.html中有段脚本,定义了一个函数 alertThis(),他会生成一个警告提示窗口。我们通过注册一个事件:onclick来触发这个函数,这个是一般的做法。假设在parent.html中有一个iframe页面iframe.html。我们希望在iframe.html中同样可以注册一个onClick事件来触发父页面中的alertThis()。但由于这是一个警告窗口,处于对用户体验的一致性,我们希望他是在parent.html页面中执行,以保证他得到同样效果和位置的一个警告提示,如果在iframe.html中则很可能做不到。这样的一个典型应用就是在网站**后台中。
范例代码:
function alertThis(){} function alertControl(){ if(parent&&parent.location==self.location){ alertThis();//这里在当前页面中执行该函数 }else{ top.alert();//这里在父页面中执行该函数 } } //注册onClick事件,执行alertControl()*也许不需要判断是子窗口还是父窗口,应该都可以运行top.alert()。因为子页面的top对象是他的父页面,而父页面的top对象,就是他自己本身?但是这样会不会有其他问题?还要进一步验证。
Leave a comment