function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocks_SFDCRocks_SFDC 

How to get the output values from Java script into Controller

Hi Team,

 

we have written a javascript in visualforce page. how to get the variables "latitude" and "longitude" from it.

 

<script>
function xyz(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(pos){
var latitude=pos.coords.latitude;
var longitude=pos.coords.longitude;
}

</script>

 

Thanks,

Anil

Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Hi use following code:

ID = ''p1' and id='f1' are important. Do not change while youtest it.  It is working fine I have tested it

<apex:page controller="test2" sidebar="false" standardStylesheets="false" showHeader="false" ID="p1">
<script>
    function reqLoc() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(findMe);
        }
        else{
            alert('Geolocation is not supported');
      }
    }
    function findMe(pos) {
    var hiddenRep1 = document.getElementById('p1:f1:hdnRep1');
    var hiddenRep2 = document.getElementById('p1:f1:hdnRep2');
        var lat = pos.coords.latitude;
        var long = pos.coords.longitude;
        var acc = pos.coords.accuracy;
        //alert("Your latitude is: " + lat + " and your longitude is: " + long + " within " + acc + " meters.");    
        hiddenRep1.value = lat;
        hiddenRep2.value = long;
    alert(hiddenRep1.value);
    alert(hiddenRep2.value);    
    callFun();
}
</script>


<apex:form id="f1">
<apex:inputHidden id="hdnRep1" value="{!lat}" />
<apex:inputHidden id="hdnRep2" value="{!lon}" />
<apex:actionFunction name="callFun" action="{!Say}"/>

<a href="javascript&colon;void()" onClick="reqLoc()">Where am I?</a>
</apex:form>

</apex:page>

 

 

 

 

All Answers

gbu.varungbu.varun

Hi,

 

Define two String lat and lon  in the controller and pass them as inputHidden. you will get values in these two variables. Try my code, If you have any problem let me know.

 

 

<script>
var hiddenRep1 = document.getElementById('pgId:frmId:hdnRep1');
var hiddenRep2 = document.getElementById('pgId:frmId:hdnRep2');

function xyz(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(pos){
var latitude=pos.coords.latitude;
var longitude=pos.coords.longitude;
hiddenRep1 = latitude.value;
hiddenRep2 = longitude.value;
}
</script>

 

<apex:inputHidden id="hdnRep1" value="{!lat}" />
<apex:inputHidden id="hdnRep2" value="{!lon}" />

 

 

Rocks_SFDCRocks_SFDC

Hi Varun,

 

Thank you for you response.

 

I have modified my code as follows. But it is not working.

 

In Visualforce Page:

  

var hiddenRep1 = document.getElementById('pgId:frmId:hdnRep1');
     var hiddenRep2 = document.getElementById('pgId:frmId:hdnRep2');
  <body onload="xyz();">
 <script language="javascript" type="text/javascript">
        function xyz(){
            if(navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(showPosition);
            }
         }
        function showPosition(pos){
            var latitude=pos.coords.latitude;
            var longitude=pos.coords.longitude;
            hiddenRep1 = latitude.value;
            hiddenRep2 = longitude.value;
    
}

</script>
<apex:inputHidden id="hdnRep1" value="{!lat}" />
<apex:inputHidden id="hdnRep2" value="{!lon}" />

 

In Controller:

Public class ClassName{

-----------------------------

---------------------------

-----------------------------

  Public String lat{get;set;}
     Public String lon{get;set;}

 

}

 

Could you please check it once.

 

Thanks,

Anil

gbu.varungbu.varun

 

 

Hi, Create a method say, and call it by actionFunction.

 

You can call your Jscript now by a button or using onchange or oncomplete etc functions;


<script>
var hiddenRep1 = document.getElementById('pgId:frmId:hdnRep1');
var hiddenRep2 = document.getElementById('pgId:frmId:hdnRep2');

function xyz(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(pos){
var latitude=pos.coords.latitude;
var longitude=pos.coords.longitude;
hiddenRep1 = latitude.value;
hiddenRep2 = longitude.value;
}
callFun();
</script>


<apex:inputHidden id="hdnRep1" value="{!lat}" />
<apex:inputHidden id="hdnRep2" value="{!lon}" />
<apex:actionFunction name="callFun" action="{!Say}"/>

and call above Jscript

______________________________________________________________

Public class ClassName{
Public String lat{get;set;}
Public String lon{get;set;}
public void say(){
System.debug(lat);
System.debug(lon);
}
}

 

Rocks_SFDCRocks_SFDC

Hi Varun,

 

Just now i have checked it through the debug logs.

 

Debug logs are not showing debug statements which we have used in "Say" method.

 

Thanks,

Anil

gbu.varungbu.varun

Hi I am giving you my code. It is working fine. If it does not work. Please letme know. 

 

<apex:page controller="test2" sidebar="false" standardStylesheets="false" showHeader="false" ID="p1">
<script>

    function reqLoc() {
        if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(findMe);
        }
        else{
            alert('Geolocation is not supported');
      }     
    }
    function findMe(pos) {
    var hiddenRep1 = document.getElementById('p1:f1:hdnRep1');
    var hiddenRep2 = document.getElementById('p1:f1:hdnRep2');
        var lat = pos.coords.latitude;
        var long = pos.coords.longitude;
        var acc = pos.coords.accuracy;
        //alert("Your latitude is: " + lat + " and your longitude is: " + long + " within " + acc + " meters.");    
        hiddenRep1 = lat;
        hiddenRep2 = long;
    alert(hiddenRep1);
    alert(hiddenRep2);    
    callFun();
}
</script>


<apex:form id="f1">
<apex:inputHidden id="hdnRep1" value="{!lat}" />
<apex:inputHidden id="hdnRep2" value="{!lon}" />
<apex:actionFunction name="callFun" action="{!Say}"/>

<a href="javascript&colon;void()" onClick="reqLoc()">Where am I?</a>
</apex:form>
</apex:page>




public class test2 {
Public String lat{get;set;}
Public String lon{get;set;}
public void say(){
System.debug(lat);
System.debug(lon);
}

}

Rocks_SFDCRocks_SFDC

Hi Varun,

 

Thank you for your support.

 

Its shows debug logs as below but The page is continoulsy loading. My Modified code below.

 

 <body onload="loadFunc();xyz();">
 <script language="javascript" type="text/javascript">
       function xyz(){
            if(navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(showPosition);
            }
         }
        function showPosition(pos){
            var hiddenRep1 = document.getElementById('page:frmId:hdnRep1');
            var hiddenRep2 = document.getElementById('page:frmId:hdnRep2');
            var latitude=pos.coords.latitude;
            var longitude=pos.coords.longitude;
             hiddenRep1 = latitude;
             hiddenRep2 = longitude;
callFun();    
      
}
</script>

</body>

 

 

10:43:39.038 (38280000)|USER_DEBUG|[143]|DEBUG|UUUUUUU
10:43:39.038 (38287000)|SYSTEM_METHOD_EXIT|[143]|System.debug(ANY)
10:43:39.038 (38301000)|METHOD_ENTRY|[144]|01pe00000009TME|RequestAccessForAccount_Controller.__sfdc_lon()
10:43:39.038 (38316000)|METHOD_EXIT|[144]|01pe00000009TME|RequestAccessForAccount_Controller.__sfdc_lon()
10:43:39.038 (38327000)|SYSTEM_METHOD_ENTRY|[144]|System.debug(ANY)
10:43:39.038 (38332000)|USER_DEBUG|[144]|DEBUG|VVVVVV

It is not showing any values

 

 

Controller Code:

 

       public PageReference Say() {
                System.debug('UUUUUUU' +lat);
                System.debug('VVVVVVV' +lon);
 
        return null;
    }
 

Thanks,

Anil

gbu.varungbu.varun

Hi use following code:

ID = ''p1' and id='f1' are important. Do not change while youtest it.  It is working fine I have tested it

<apex:page controller="test2" sidebar="false" standardStylesheets="false" showHeader="false" ID="p1">
<script>
    function reqLoc() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(findMe);
        }
        else{
            alert('Geolocation is not supported');
      }
    }
    function findMe(pos) {
    var hiddenRep1 = document.getElementById('p1:f1:hdnRep1');
    var hiddenRep2 = document.getElementById('p1:f1:hdnRep2');
        var lat = pos.coords.latitude;
        var long = pos.coords.longitude;
        var acc = pos.coords.accuracy;
        //alert("Your latitude is: " + lat + " and your longitude is: " + long + " within " + acc + " meters.");    
        hiddenRep1.value = lat;
        hiddenRep2.value = long;
    alert(hiddenRep1.value);
    alert(hiddenRep2.value);    
    callFun();
}
</script>


<apex:form id="f1">
<apex:inputHidden id="hdnRep1" value="{!lat}" />
<apex:inputHidden id="hdnRep2" value="{!lon}" />
<apex:actionFunction name="callFun" action="{!Say}"/>

<a href="javascript&colon;void()" onClick="reqLoc()">Where am I?</a>
</apex:form>

</apex:page>

 

 

 

 

This was selected as the best answer
Rocks_SFDCRocks_SFDC

Hi Varun,

 

We are calling "callFun" inside the function. if we use like that, then the page is loading

continuously.

 

Could you please send the code that have calling the function "callFun" outside the <apex:form> tags.

 

Thanks,

Anil

 

gbu.varungbu.varun

Hi Anil, 

 

your question is not clear to me. My function "callFun"  is also in the Function.

Thomas.KurtzThomas.Kurtz

Anil,

 

Look carefully at Varun's two code posts.

 

The early one, which you show you are using, does not set the values of the elements, but instead overwites the variables.

Make sure that your code is setting the value like hiddenRep1.value = lat;  not just hiddenRep1 = lat;

I tested the orginal code like you did and found no values getting set. I changed the hidden input to a text input and saw on the page they were not being set after adding the .value the code works as expected.

 

        hiddenRep1.value = lat;
        hiddenRep2.value = long;
    alert(hiddenRep1.value);
    alert(hiddenRep2.value);    

 

Tom

gbu.varungbu.varun

Hi Anil,

 

 

I think you have some another requirement.