You need to sign in to do that
Don't have an account?
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
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
All Answers
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}" />
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
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);
}
}
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
Hi I am giving you my code. It is working fine. If it does not work. Please letme know.
public class test2 {
Public String lat{get;set;}
Public String lon{get;set;}
public void say(){
System.debug(lat);
System.debug(lon);
}
}
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>
It is not showing any values
Controller Code:
public PageReference Say() {
System.debug('UUUUUUU' +lat);
System.debug('VVVVVVV' +lon);
return null;
}
Thanks,
Anil
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
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
Hi Anil,
your question is not clear to me. My function "callFun" is also in the Function.
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.
Tom
Hi Anil,
I think you have some another requirement.