rostam bapari
rostam bapari

Reputation: 1

Value unexpectedly changes while sending from jsp to javascript function

I have a piece of code in jsp:

String temp=new SimpleDateFormat("MMddyyyy").format((java.sql.Date)ppdates.get(authShown));
out.print(temp);

<select id="pcol<%=i%><%=weekNo%><%=au%>" onChange="pSelectedAuth(<%=i%>,<%=weekNo%>,<%=au%>,<%=currentEmployee%>,<%=temp%>)">

This prints 06042012 on the screen.

Now, my javascript functions are below:

function pSelectedAuth(formID,weekNo, index, currentEmployee,startDate){
alert(formID+":"+weekNo+":"+index+":"+currentEmployee+":"+startDate);

onchange, this alert shows 1623050 Does anyone has any idea how to get my 06042012 back?

Upvotes: 0

Views: 55

Answers (1)

ruakh
ruakh

Reputation: 183321

You're not doing anything to quote your arguments (that is — you're not wrapping them in '...' or "..."), so they're being interpreted as JavaScript expressions. In JavaScript source-code, 06042012 is interpreted as a base-8 integer (because of the leading 0), so it denotes 1623050.

To fix this, be sure to wrap your JavaScript strings in '...' or "..." (as well as to properly escape any internal quotation-marks, backslashes, newlines, special characters, </, and so on). That way, you'll have '06042012' or "06042012", which JavaScript will interpret as a string, like you want.

Upvotes: 1

Related Questions