tags:

views:

68

answers:

1

Hi,my problem is attribute onclick in tag a.I want to do scriplet command in attribute onclick.When I write this...

<a href="somethig" onclick="session.setAttribute('newAttribute','value')" >Click on me</a>

...so nothing happens (session's attribute is not set), and if I write this one

<a href="somethig" onclick="<% session.setAttribute('newAttribute','value'); %>" >Click on me</a>

so, session's attribute is set now (not when I clik on it).

(Sorry for my english, I'm beginner :)

+3  A: 

You are confusing the role of client side code and server side code.

The 'onclick' event of an anchor tag will execute on the client only.

Your code will be sent to the client as:

<a href="somethig" onclick="" >Click on me</a>

But will execute the following on the server:

session.setAttribute('newAttribute','value');

The <% %> tags denote that the code runs on the server, not on the client.

Therefore, in order for this to work you need to execute some kind of javascript to 'call back' to the server in order to signal that the link has been clicked. This is usually done through an AJAX call to another page on the server.

Codebrain