jsp中通过java访问javascript

发布于 2024-11-02 02:53:16 字数 372 浏览 0 评论 0原文

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

<%
    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>

How do I call the javascript function inside the Java code?或者说这是不可能的?

My code currently looks like this

<%
    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>

How do I call the javascript function inside the Java code? Or is that not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲歌长辞 2024-11-09 02:53:16

JSP 在网络服务器上运行,并根据网络浏览器请求生成/生成 HTML/CSS/JS 代码。网络服务器将 HTML/CSS/JS 发送到网络浏览器。 Web 浏览器运行 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>

或者,更好,使用 EL

<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>

(请注意我将属性名称更改为 foo 因为 request 代表 HttpServletRequest 这可能会让其他人感到困惑,因为它永远不会 null

无论哪种方式,当条件为真时,生成的 HTML(您应该通过在浏览器中打开页面,右键单击它并选择查看源代码看到)应该如下所示:

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

现在是否打开你头顶上有一个灯泡吗?

JSP runs at webserver and generates/produces HTML/CSS/JS code upon a webbrowser request. Webserver sends HTML/CSS/JS to webbrowser. Webbrowser runs HTML/CSS/JS. So, you just have to let JSP print it literally as JS code.

<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>

or, better, with EL

<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>

(note that I changed the attribute name to foo because request stands for HttpServletRequest which might confuse others since this is never null)

Either way, the generated HTML (which you should see by opening page in browser, rightclicking it and choosing View Source) should look like this when the condition was true:

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

Does it now turn on a light bulb above your head?

欲拥i 2024-11-09 02:53:16

你不能从java调用javascript函数

你的java代码在服务器上执行,而javascript在客户端上执行。

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

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

上面是用于检测文档加载的 jQuery。您可以将其替换为纯 javascript - window.onload = function() {..}document.onload = function() { ..} 我认为)

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

最后 - 使用 jstl 标签(就像我展示的那样)而不是 java 代码(scriptlet)。

You can't call javascript functions from java

Your java code is executed on the server, and the javascript - on the client.

What you seem to need is conditionally open a new window on document load. For that:

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

(that's jQuery above for detecting document load. You can replace it with pure javascript - window.onload = function() {..} or document.onload = function() {..} I think)

Note that request != null is meaningless condition - the request is never null in a JSP.

And finally - use jstl tags (like I showed) and not java code (scriptlets).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文