Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to show a javascript 'var' in my jsp?

 ...
 <script type="text/javascript">
 ... // My code to get the value. 
 var val = combo.getValue(); 
 </script>
 <body>
 The value is : //to be displayed here
 </body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
144 views
Welcome To Ask or Share your Answers For Others

1 Answer

Add a HTML element which should mark the place where the value is to be displayed and give it an id.

<body>
    The value is : <span id="value"></span>
</body>

Then let your JS access it by document.getElementById() and modify its inner HTML.

document.getElementById("value").innerHTML = val;

You only need to ensure that the particular script executes after the HTML page has loaded. Do it during window.onload or put the <script> at end of <body> or wrap it in a function which you execute on some event.

As to the JSP part, this is not relevant here. All JSP does is generating and sending HTML/CSS/JS code from webserver to webbrowser. JavaScript knows nothing about JSP, all it can see and access is HTML/CSS which you can also see by rightclicking the page in webbrowser and choosing View Source.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...