[Html&JS] 一个网页上,不同的子父窗口iframe之间如何相互调用

来源:互联网 发布:购物系统源码 编辑:程序博客网 时间:2024/06/04 20:14

最近有个需求是,如图。有一个Help的网页,用户在不同的页面时候,点击? button。此时跳转到help页面的当前页的帮助文档。
Like:当前页面LED,点击上方的?按钮,跳转到help页面的LED处。

其实在help页面处添加一个ID

<a name='LED' id="LEDID"></a>

?按钮的url修改为即可。
help.htm#LEDID

这里写图片描述


但是问题是这套代码是用的三个ifrme构成。如图:

这里写图片描述

现在的思路就是每次用户点击menu栏,更新mian里面的内容。再在main里面修改iframe_top里面的?按钮的url的链接即可。

在index页面中,iframe_top的name为topFrame

<div>    <iframe name="topFrame" src="setup_top.htm"></iframe></div>

在iframe_main中,window.parent.frames["topFrame"]
可以先获取iframe_main的父窗体。然后再取父窗体的frames[“topFrame”],即iframe_top

就可以更新?的url链接。此时在点击?即可跳转到help页面的LEDID的内容了。
如下:

window.parent.frames["topFrame"].document.getElementById("HelpBtId").href = 'help.htm#LEDID';

ps:因为很多页面总不能都每个页面都添加吧。发现所有页面都添加了global.js,并且每个页面都有一init的函数。所有在js里面添加一个函数,然后再每个网页的init中添加该函数就行了。

<script language="javascript" src="js/global.js"></script>

在该js添加一个函数

/*    Set the link to the Help button, Different pages for different HelpUrl.For different Help content.    reSet the topFrame(setup_top.htm / <a href='help.htm' id=HelpBtId>) Help button url.    helpurl var like:LEDID,SystemLogID.(LEDID define in help.htm)  reSetHelpUrl(LEDID)  */function reSetHelpUrl(helpurl){    if(helpurl != "")    {        var setHelpUrl = "help.htm#" + helpurl; //help.htm#LEDID        window.parent.frames["topFrame"].document.getElementById("HelpBtId").href = setHelpUrl;    }    else    {        return;    }}

init中调用为:

reSetHelpUrl("LEDID");

不同的页面只需修改函数参数就ok了。
这里写图片描述

0 0