This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes jsp with javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp with javascript" Watch "jsp with javascript" New topic
Author

jsp with javascript

simran chandwani
Greenhorn

Joined: Jun 29, 2009
Posts: 2
This code is working ok. But when I replace int with String throws error.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%

int i=0;
String str=null;
while(i<10)
{i++;
str="abc"+i;
%>

<input type="text" id="aa" onclick="alert(<%=i%>);" />
// <input type="text" id="aa" onclick="alert(<%=str%>);" /> this is not working


<%
}
%>
<script type="text/javascript">

function total(str)
{

alert(str);

}

</script>
</body>
</html>
This code is working ok . But when I replace i with str [for eg alert(<%=str%>)] this shows str is not defined. But this code is working ok with i [integer value]



ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
In the case of Integer, you are passing the int value which is a number.
In the case of String, you are passing the runtime value which is some text representation.

The alert() function doesn't know how to interpret the text value unless its a String,
it assumes you are trying to print a JavaScript variable which in your case doesn't exist.

However to get it to work you will have to enclose the value in single quotes.


You can investigate this further by viewing the source of the JSP file from within the browser.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp with javascript
 
Similar Threads
how to create a table through ajax dynamically
Sending checkbox value to two different servlet page
Get Rows from a Table
accessing jsp array variable in javascript
Cannot see the JavaScript code I wrote when I hit script tab on firebug :(