简体   繁体   中英

call a javascript function inside tag library jsp

How to call a javascript function in jsp within tab library. I have tried something like, below is my code.

JavaScript function:

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

JSP code:

<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). So what you've done there is actually output myconfirmbox("hello"); as text on the page.

If you want it to be run as inline script, add <script> tags around it. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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