日期:2014-05-16  浏览次数:20515 次

iframe下,子页面调用父页面js方法

我要实现的功能是这样的,test.jsp是一个饼状图,是别人做好的完整的页面,我想在我的页面index.jsp中嵌入test.jsp

当然就用到了iframe了,但是这样就有个问题,子页面中的饼状图是自动刷新的,父页面无法实时的把数据传给子页面,

我的办法是:在子页面中调用父页面中js方法,获取返回值。

具体代码如下:

index.jsp:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
	//这是子页面将要调用的方法
  function f(){
   return "yyy,90,kkk,100,uu,80";
 }
</script>
<body>
<table width="200" border="1">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><iframe  scrolling="yes" src="test.jsp" width="600" height="600"></iframe><!--这里是我要加入的页面-->
</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
?

test.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >    
  
<HTML xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">   
    <HEAD>   
        <title>饼状图</title>   
        <Meta http-equiv="Refresh" Content="10">   
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">   
        <meta name="vs_defaultClientScript" content="JavaScript">   
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">   
        <STYLE> v\:* { Behavior: url(#default#VML) }   
    o\:* { behavior: url(#default#VML) }   
        </STYLE>   
        <SCRIPT LANGUAGE="JavaScript">   
        <!--   
            var onit=true;   
            var num=0;   
            var stat_array = new Array();   
               
            function moveup(iteam,top,txt,rec)   
            {   
                temp = eval(iteam);   
                tempat = eval(top);   
                temptxt = eval(txt);   
                temprec = eval(rec);   
                at = parseInt(temp.style.top);   
                temprec.style.display = "";    
                if (num > 27)   
                {   
                    temptxt.style.display = "";   
                }   
                if(at>(tempat-28) && onit)   
                {   
                    num++;   
                    temp.style.top = at-1;   
                    Stop=setTimeout("moveup(temp,tempat,temptxt,temprec)",10);   
                }   
                else  
                {   
                    return;   
                }    
            }   
               
            function movedown(iteam,top,txt,rec)   
            {   
                temp = eval(iteam);   
                temptxt = eval(txt);   
                temprec = eval(rec);   
                clearTimeout(Stop);   
                temp.style.top = top;   
                num = 0;   
                temptxt.style.display = "none";   
                temprec.style.display = "none";   
            }   
               
            function ontxt(iteam,top,txt,rec)   
            {   
                temp = eval(iteam);   
                temptxt = eval(txt);   
                temprec = eval(rec);   
                if (onit)   
                {   
                    temp.style.top = top-28;   
                    temptxt.style.display = "";   
                    temprec.style