• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling javascript function from jsp  RSS feed

 
Richard Ellison
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a jsp with a javascript function.The jsp has a struts based logic iterate tag in it.All I am trying to do is to pass an int value to the javascript function.

The code snippet is something like this:

<jsp:scriptlet>
int i=0;
</jsp:scriptlet>

<html:link styleClass="menu" href = "javascript:testFunc(<%=i%>)" >

[Add]</html:link>



The javascript Function is :

function testFunc(a){
alert("test"+a);

}


But for some reason I get a javascript error.When I do a mouse over on the "Add" link above,the javascript function is being called as
javascript:testFunc(<%=i%>) rather than javascript:testFunc(0).

I know this looks pretty simple but I am not able to figure it out.

Please advise.

Thanks
Richard
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66156
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the time the HTML page gets to the browser where the JavaScript will be evaluated, all JSP expressions should have been evaluated on the server. Do a View Source at the browser. Is the HTML as you expect?

P.S. Scriptlets have been discredited for almost 10 years now. If this isn't a legacy page, you should be using modern mechanisms.

P.P.S. Please UseCodeTags when posting code. It makes it a lot more readable.
 
Richard Ellison
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear for your response.

The jsp is expressed something like this:

<td >[Add]</td>

It should have been
<td >[Add]</td>

where 0 is the value of i.

Thanks

Richard
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!