javascript onclick in html confusion
I was going through some java
code(which uses playframework
) that provides some shopping cart functionality.The html
page contains some javascript
(it uses jquery
) which is something I have very little knowledge of.Looking through the html,I found a table like this
<table>
...
<td>
<a href="" onclick="return addItem(${item.id});">buy</a>
</td>
...
</table>
(The template uses ${} to get dynamic values thru playframework template language)
In the entire code ,the only addItem(itemId)
method I could find was in the ShoppingCart
class which has this signature
public void addItem(Item item) {
...
}
When I searched for onclick ,I found this page onclick doc,which gives the syntax of onclick as
onclick="SomeJavaScriptCode"
Going thru all the javascript code(it uses jq开发者_运维问答uery and ejohn-templating)I couldn't find any method like 'addItem'
So,how does the onclick
work?How would it know which method to invoke?Will it look in the entire code (javascript and java)to find a matching method?
somewhere you have an addItem
method in your javascript code. The code in the html is executing that method, not the addItem
method you found in the java code.
First is it would only look for javascript methods and not java methods. Second is you can use debugging in firebug to reach to the method called.
It is relatively simple. It looks like your Java code is executed on the server side, and the JavaScript (including jQuery) is executed on the client side. So the ${item.id} is executed on the server, and onClick executes whatever javascript code you provide for it on the client side.
I do not know playframework, but the addItem
should be javascript, and public void addItem(Item item)
is java code. Both are not related
This addItem
javascript function is somewhere. If you are unable to find it in your code base, you can use the console of your browser (or firebug on Firefox) to see this javascript function. Go to the Script tab and search for addItem.
Javascript can't access server side functions unless is via AJAX or something like it. Even then, it doesn't have direct access to the funcions, but to a page that access that.
Look more carefully for that function in your Javascript using firebug, or the google chrome console. It should be there.
精彩评论