• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

how to send java variable to javascript

I want the java variable value in my javascript, example below i want "sample" in alert not "str"
<html>
<body>
<% String str = "sample";%>
<script TYPE="text/javascript" LANGUAGE="JavaScript">
	window.onload = function (){
		var value = "<%=str%>";
		alert(value);
	}
</script>
</body>
</html>

Open in new window

0
Adwait Chitaley
Asked:
Adwait Chitaley
  • 2
1 Solution
 
JoachimMartinsenCommented:
Try this:
<html>
<body>
<% str = "sample" %>
<script TYPE="text/javascript" LANGUAGE="JavaScript">
        window.onload = function (){
                var value = "<%=str%>";
                alert(value);
        }
</script>
</body>
</html>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
Joachim: Why would that work?

achitaley: Your method is correct, but the page needs to be a JSP or similar
0
 
Michel PlungjanIT ExpertCommented:
???
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now