Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jsp中通过java访问javascript_Java_Javascript_Jsp - Fatal编程技术网

在jsp中通过java访问javascript

在jsp中通过java访问javascript,java,javascript,jsp,Java,Javascript,Jsp,我的代码目前看起来像这样 <% if (request != null) { bustOut; } %> <script language="javascript"> function bustOut(){ var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes"); } <

我的代码目前看起来像这样

<%
    if (request != null) {
        bustOut;
    }
%>

<script language="javascript">
function bustOut(){
   var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes"); 
}
</script>

函数bustOut(){
var newWin=window.open(“实际url”,“子窗口”,“高度=500,宽度=700,可调整大小=yes,滚动条=yes”);
}

如何在Java代码中调用javascript函数?或者这是不可能的?

您不能从java调用javascript函数

java代码在服务器上执行,javascript代码在客户端上执行

您似乎需要的是在加载文档时有条件地打开一个新窗口。为此:

<c:if test="${shouldDisplayWindow}">
     $(document).ready(function() {
         bustOut();
     });
</c:if>

$(文档).ready(函数(){
bustOut();
});
(这是上面用于检测文档加载的jQuery。您可以用纯javascript替换它-
window.onload=function(){..}
document.onload=function(){..}

请注意,
request!=null
是无意义的条件-请求在JSP中永远不会
null


最后-使用jstl标记(如我所示)而不是java代码(scriptlets)。

JSP在webserver上运行,并根据webbrowser请求生成HTML/CSS/JS代码。Web服务器向webbrowser发送HTML/CSS/JS。Webbrowser运行HTML/CSS/JS。所以,您只需要让JSP将其按JS代码的字面形式打印出来

<script language="javascript">
    function bustOut(){
       var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes"); 
    }
    <% 
        if (foo != null) { 
            out.print("bustOut();");
        }
    %>
</script>
现在它会打开你头顶上的灯泡吗

<script language="javascript">
    function bustOut(){
       var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes"); 
    }
    ${not empty foo ? 'bustOut();' : ''}
</script>
<script language="javascript">
    function bustOut(){
       var newWin = window.open("the real url", "subWindow","height=500,width=700,resizable=yes,scrollbars=yes"); 
    }
    bustOut();
</script>