今天由于客户对功能提出新要求。采用js操作iframe解决问题。特记录其中遇到的问题。
用Js控制iframe内表单提交
代码如下:
 1 <% @ page language = " java "   import = " java.util.* "  pageEncoding = " GB18030 " %>
 2 <! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
 3 < html >
 4    < head >     
 5      < title > c.jsp </ title >
 6     
 7      < meta http - equiv = " pragma "  content = " no-cache " >
 8      < meta http - equiv = " cache-control "  content = " no-cache " >
 9      < meta http - equiv = " expires "  content = " 0 " >     
10      < meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
11      < meta http - equiv = " description "  content = " This is my page " >     
12
13      < script type = " text/javascript " >
14      <!--
15         function reg() {
16        jsi.document.all.username.value="xxx";
17        jsi.document.all.userid.value="xxx>";
18        jsi.document.all.flag.value="xxx";
19          jsi.document.forms["jsf"].submit();        
20          window.open("http://js.51baojian.net","_blank");  
21          setTimeout("",3000);
22          window.location.href="http://jss.51baojian.net";
23    }

24      -->
25      </ script >
26    </ head >
27   
28    < body onload = " reg(); " >
29      < table cellpadding = " 0 "  cellspacing = " 0 "  border = " 0 " >
30          < tr >< td >
31             页面进入中..
32              < iframe id = " jsi "  name = " jsi "  frameborder = " 0 "
33                     src = " b.jsp "  height = " 0 "  width = " 0 "
34              ></ iframe >
35          </ td ></ tr >         
36      </ table >
37    </ body >
38 </ html >
b.jsp代码为:
1
2 < form id = " jsf "  name = " jsf "  action = " url " >
3      < input type = " hidden "  name = " username "  value = "" >
4      < input type = " hidden "  name = " userid "  value = "" >
5      < input type = " hidden "  name = " flag "  value = "" >                 
6 </ form >
7
注意:
页面加载时,遇到 iframe 就直接跳过去,加载下面的内容,然后再回来加载 iframe ,当然也可以理解成遇到 iframe 又开了一个线程来加载 iframe ,但是因为涉及到新的 IO 操作比较耗时,所以如果在iframe下写了js操作的话,会提示错误。可以采用onload来触发js操作执行。
在注意:(知道的就不要看了)

onload指整个页面加载完成后再执行init()函数,而不是当页面加载到<body>时就开始执行。

同样,onunload指整个页面卸载完成(关闭)后。