To call a controller method (Apex function) from JavaScript, you need to use actionfunction.
Look at the below piece of code to understand how a controller method is called using actionfunction.
1
2
3
4
5
6
<script>
function JSmethodCallFromAnyAction()
{
callfromJS();
}
</apex:page>