Java + Javascript : How to take a user location by browser ?

In this example when the user clicks anywhere inside this form his location will be obtained by the javascript function.

I am by setting the value in a hidden field to I have how to access this value in the backend, that is, in my controller or bean.

Full Example :

<script type="text/javascript">
    $(document).ready(function() {
        myIP = function(){
            navigator.geolocation.getCurrentPosition(function(position) {
                var valor  = position.coords.latitude + ',' + position.coords.longitude;
                $( ".latlong").val(valor);
            return false;
<h:form id="form" onclick="myIP()">
    <h:inputText styleClass="latlong" style="display:none" id="latlong" value="#{myController.latlong}"/>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s