简体   繁体   English

在标签库jsp中调用javascript函数

[英]call a javascript function inside tag library jsp

How to call a javascript function in jsp within tab library. 如何在标签库中的jsp中调用javascript函数。 I have tried something like, below is my code. 我已经尝试过类似的东西,下面是我的代码。

JavaScript function: JavaScript函数:

    function myconfirmBox(msg){ alert(msg);}

JSP code: JSP代码:

<s:if test="Messages()">
            myconfirmBox("hello");
        </s:if>

What am i actually doing wrong? 我实际上在做什么错?

The JSP stuff runs on the server , your JavaScript code will run on the client (unless you're trying to integrate with Rhino, which I suspect is not the case). JSP东西在服务器上运行,您的JavaScript代码将在客户端上运行(除非您尝试与Rhino集成,我怀疑情况并非如此)。 So what you've done there is actually output myconfirmbox("hello"); 因此,您在那里所做的实际上是输出myconfirmbox("hello"); as text on the page. 作为页面上的文本。

If you want it to be run as inline script, add <script> tags around it. 如果希望将其作为嵌入式脚本运行,请在其周围添加<script>标记。 But more likely you need to step back and take a look at the wider context, you probably want to wait for some kind of event to occur (even if it's just that the page is completely loaded) before showing a confirm box. 但是,您更有可能需要退后一步,并查看更广泛的上下文,您可能想要在显示确认框之前等待某种事件发生(即使只是页面已完全加载)。

When writing dynamic server-side code, you always have to think about what will actually get sent to the browser to be interpreted. 在编写动态服务器端代码时,您始终必须考虑将实际发送到浏览器的内容以进行解释。 You can use tools like the Network tab in Chrome's Developer Tools to see what you're actually sending, so you can see what the browser is working with. 您可以使用Chrome开发者工具中的“网络”标签之类的工具来查看实际发送的内容,以便查看浏览器正在使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM