最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - calling Javascript function in JSP - Stack Overflow

programmeradmin5浏览0评论

I'm trying to call a function and activate an alert through it in my JSP.
This is what i've done so far:

<html>
<head>
<script type="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

function myFunction( test )
{
    alert( test );
}
</script>
<title>Success</title>
</head>
<body>
     <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
     <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test">
</body>
</html>

What is wrong with my code?

I'm trying to call a function and activate an alert through it in my JSP.
This is what i've done so far:

<html>
<head>
<script type="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

function myFunction( test )
{
    alert( test );
}
</script>
<title>Success</title>
</head>
<body>
     <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
     <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test">
</body>
</html>

What is wrong with my code?

Share Improve this question asked Aug 21, 2013 at 7:18 newbienewbie 5372 gold badges8 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You had a meta element inside script element which will throw an error while parsing the script block

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <script type="text/javascript">

            function myFunction( test )
            {
            alert( test );
            }
        </script>
        <title>Success</title>
    </head>
    <body>
        <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
        <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test" />
    </body>
</html>

In addition to that, You might also need to add a single quote inside the calling function

<input type="button" id="sample_button" onclick="myFunction('${test.userName}')" value="test">

Check for javascript errors & verify the generated HTML in browser

发布评论

评论列表(0)

  1. 暂无评论