jsp2:
<%@ page language="java" contentType="text/html;charset=GBK" errorPage="ErrorPage.jsp"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body>
<form name="c"  method="post" action="test.jsp">
<label>
<%
String avalue = request.getParameter("b");
if (avalue!=null){
out.println(avalue);
}
%>
</label>
<input type="hidden" name="b">
</form>
</body>
</html>如果我把 testwin() 这个函数中的alert()去掉,那么10次中会有3-5次的找不到对象,如果加上,每次都不会出现,大家能告诉我错在哪里么?

解决方案 »

  1.   

    function testwin(){
    var a = window.open("test.jsp","aa");//返回新打开的窗口

    alert("a:"+a);//返回的是一个对象
    //alert("a.c"+a.document.c);也是一个对象,新窗口中(form)
    //alert("a.c.b"+a.document.c.b);也是一个对象,新窗口中(form中的text)
    //for (var i=0;i<100;i++){

    //}
    a.document.c.b.value = "11";
    a.document.c.submit();//新窗口中form提交
    }
    </script>
      

  2.   

    同意,应该是说找不到对象的那几次,jsp2还没有打开呢!
    你可以将jsp1中要进行的动作放到jsp2中去执行啊!
      

  3.   

    但为啥加上alert()就可以了呢?而且alert()的时候并没有说是undefined啊!
      

  4.   

    加上if(a.readState=='complete')//装载完后再触发
    ....
      

  5.   

    每次alert()都能正确返回值?????
    还是劝你将那个提交表单的动作放到jsp2中去。
    何苦呢?为了一个完全可以解决的方法纠缠?
    佩服你的精神,但是……
    sorry,每见过这种情况。
      

  6.   

    if(a.document.readState=='complete')//
      

  7.   

    楼主的jsp2 代码不会就是test.jsp吧
    如果是,你一辈子都得不到他的值alert("a:"+a);
    后就要提交jsp1  1.jsp open 2.jsp
    jsp2  2.jsp action="test.jsp"
      test.jsp 中 request.getParameter("b");
       得到值 11
      

  8.   

    jsp2 就是“test.jsp”,而且加上alert是肯定可以得到的,没有alert()有时会得不到,我就是想去掉得不到的情况!
      

  9.   

    <script>
    var a=null;
    function testwin(){
    a = window.open("test.jsp","aa");
             submit1();
            }
    else
    {
    setTimeout("submit1();",50);
    }
    }
    function submit1(){
    if(a.document.readyState=='complete'){
    a.document.c.b.value = "11";
    a.document.c.submit();
    }
    }
    </script>
      

  10.   

    <script>
    var a=null;
    function testwin(){
    a = window.open("test.jsp","aa");
             a.document.onload=new Function("submit1()")
    }function submit1(){
    if(a.document.readyState=='complete'){
    a.document.c.b.value = "11";
    a.document.c.submit();
    }
    }
      

  11.   

    jsp1:
    <%@ page language="java" contentType="text/html;charset=GBK" errorPage="ErrorPage.jsp"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <script>
    function testwin(){

    alert("a:"+a);
    }
    </script>
    <body>
    <form>
    <input type="button" name="test" value="test" onclick="testwin();">
    </form>
    </body>
    </html>
    这个问题不是JSP的问题,
    而是IE的问题,IE6,10次中10次都没问题,但是WIN2000的IE,
    98的好象还好一些,10中,我是6次都找不到这个函数,其实,就写一个上面的样子,有的时候,也是找不到,后来发现了,一个IE的BUG,这样写:<input type="button" name="test" value="test" onclick="testwin('aa');"><script>
    function testwin(aa){

    alert("a:"+a);
    }
    </script>10次中10次好使,但不知为什么???
    我也是每回都小心地加上一个没有用的参数的
      

  12.   

    找不到对象
    有的时候,就没进去那个函数,虽然我的回答,和你的问题,真是一点边都不搭但是,你最好确信,它每次都进到函数里面了,只是进去,你的代码,好象没问题,
    其实,我对JSCRIPT也是白痴一个
      

  13.   

    也许真的应该按照阿信的法子,等到onload的时候再开始你的事件?
    或者,干脆setTimeout()执行你的函数。
      

  14.   

    昨天我试到比较晚,确实在ie6下是没有问题的,在ie5下的话,如果新打开的窗口没有焦点的话,就会正确执行,如果焦点在新窗口上,无论怎样都是会报undefined错误的,空循环和setTimeout都是没有用的,用onload事件来执行的结果也是一样的。
      

  15.   

    to  dearmite(我是笨笨!) 
      我也是新手,用jsp也才一个星期,但是这个确实还是不难的,反倒是js比较烦,经常会碰到不同版本不同结果的问题,比如用post到一个blank中,ie5.5就有opener而5.0就没有,感觉很无奈阿!总不能教所有的客户都给你升级吧?何况很多客户是啥都不懂的,只好....
      

  16.   

    回复人: zjoner(我真的不知道) ( ) 信誉:100  2003-4-4 9:02:06  得分:0 
       
    昨天我试到比较晚,确实在ie6下是没有问题的,在ie5下的话,如果新打开的窗口没有焦点的话,就会正确执行,如果焦点在新窗口上,无论怎样都是会报undefined错误的,空循环和setTimeout都是没有用的,用onload事件来执行的结果也是一样的。如果是这样的话就比较好办了,在你的jsp2中写一句话:<script>opener.focus()</script>
    或者<script>self.blur()</script>
     
     
      

  17.   

    to every one:
       刚才试了下一种新方法,问题解决了,我把方法贴出来大家看下:
    test.jsp:
      <%@ page language="java" contentType="text/html;charset=GBK" errorPage="ErrorPage.jsp"%><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <script language="javascript">
    function f_close(){
    window.opener.document.form1.btext.value = c.b.value;
    window.close();
    }
    </script>
    <body>
    <form name="c"  method="post" action="test.jsp">
    <label>
    <%
    String avalue = request.getParameter("b");
    %>
    </label>
    <input type="text" name="b" value=<%= avalue%>>
    <input type="button" name="test" value="close" onclick="f_close();" ></form>
      

  18.   

    testwin.jsp:<%@ page language="java" contentType="text/html;charset=GBK" errorPage="ErrorPage.jsp"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <script language="javascript">
    function testwin(aa){
    var a = window.open("","aa");    a.document.writeln("<html>");
    a.document.writeln("<body>");
    a.document.writeln("<form name='c'  method='post' action='test.jsp'>");
    a.document.writeln("<input type='text' name='b'>");
    a.document.writeln("</form>");
    a.document.writeln("<script language=javascript>");
    a.document.writeln("document.c.b.value='11'");
    a.document.write("</");a.document.write("script>");
    a.document.writeln("</body>");
    a.document.writeln("</html>");
    a.document.c.submit();
    }
    </script>
    <body>
    <form name="form1">
    <input type="text" name="btext"> <input type="button" name="test" value="test" onclick="testwin('aa');">

    </form>
    </body>
    </html>
      

  19.   

    用document.writeln()写了一个scipt 和 html,这样就行了!
    多谢大家的指教:
             fason(阿信) ( ) 
             xinyunyishui(心云意水) 
              dearmite(我是笨笨!) 
      

  20.   

    两点:
    1.与其写这么多代码,不按照我说这个法子试试看:
      I.jsp1中:
    <script>
       function testwin(){
    var a = window.open("test.jsp","aa");
         }
    </script>
    jsp2中:
    <script>
       function window.onload(){
    c.submit();
             window.close();
         }
    </script>
    是不是比你的短很多?
    2.其实,不要用window.open打开新窗口,用一个隐藏的iframe来做这个,应该会让客户感觉更好一点,因为大多数人都讨厌弹出的窗口。
      

  21.   

    其实,不要用window.open打开新窗口,用一个隐藏的iframe来做这个,应该会让客户感觉更好一点,因为大多数人都讨厌弹出的窗口。没想到结贴子,我现在就改用这种办法了,隐藏的iframe比传参 数都灵活,主要是,什么版本都好使!