+ Start a Discussion
AK_SmithAK_Smith 

user agent with JS on visualforce page

Hello! Please hellp me to fix my code to get user agent 
 
<apex:page standardController="CTPHARMA__Activity__c" id="page">

<script>
agent()
function agent() {
    var u = window.navigator.userAgent;
       document.getElementById('page:form:A').value  = 1;
       }
</script>




<apex:form id="form" >

<apex:inputField id="A"  value="{!CTPHARMA__Activity__c.SF1_UA__c}" /> 

</apex:form>



</apex:page>

 
Best Answer chosen by AK_Smith
piyush_sonipiyush_soni
try below code :
<apex:page standardController="CTPHARMA__Activity__c" id="page" docType="html-5.0">

<apex:form id="form" >
  <div style="display:none;">
   latitude :<apex:inputField id="lati"  value="{!CTPHARMA__Activity__c.SF1_Lati__c}" /> <br></br>
   longitude :<apex:inputField id="long" value="{!CTPHARMA__Activity__c.SF1_Long__c}" /> <br></br>
   SF1 : <apex:inputField id="SF1" value="{!CTPHARMA__Activity__c.SF1__c}" />
   Attempts : <apex:inputField id="Attempts" value="{!CTPHARMA__Activity__c.SF1_Attempts__c}" />

<apex:commandButton action="{!save}"  value="Save"/></div>

<apex:actionFunction action="{!quicksave}" name="ActionFunctionName"/>

<img src="{! IF(CTPHARMA__Activity__c.Is_confirmed_territorially__c == true, 'https://emojipedia-us.s3.amazonaws.com/thumbs/120/apple/114/thumbs-up-sign_1f44d.png','https://emojipedia-us.s3.amazonaws.com/thumbs/120/apple/114/thumbs-down-sign_1f44e.png')}" align="center"/>   <br></br>
<apex:outputText value="{!CTPHARMA__Activity__c.SF1_Status__c}"/>

</apex:form>

<script>
geoFindMe();
function geoFindMe() {
  var output = document.getElementById("out");
  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;
    var at = {!CTPHARMA__Activity__c.SF1_Attempts__c};        
    document.getElementById('page:form:lati').value = latitude.toString().replace(".",",")  ;
    document.getElementById('page:form:long').value = longitude.toString().replace(".",",")  ; 
    document.getElementById('page:form:SF1').checked = true;
    document.getElementById('page:form:Attempts').value  = (at + 1);
    callActionFunction();
   
  };

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  };

  navigator.geolocation.getCurrentPosition(success, error);
 }
   var x = {!CTPHARMA__Activity__c.SF1_Request_geo__c};
   function callActionFunction(){
   if(x){ 
     ActionFunctionName();
   }
   }
</script>
</apex:page>
let me know if it helps you
thanks


 

All Answers

AK_SmithAK_Smith
 document.getElementById('page:form:A').value = u
piyush_sonipiyush_soni
hi smith,
use below code :
<apex:page standardController="CTPHARMA__Activity__c" id="page">

<apex:form id="form" >

<apex:inputField id="A"  value="{!CTPHARMA__Activity__c.SF1_UA__c}" /> 

</apex:form>

<script>
agent();
function agent() {
    var u = window.navigator.userAgent;
    //  alert(u);
       document.getElementById('page:form:A').value  = u;
       }
</script>

</apex:page>

put your js in end of the page :
i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you
thanks
AK_SmithAK_Smith
Works stand alone but i cant to add the code to my existing page
 
<apex:page standardController="CTPHARMA__Activity__c" id="page" docType="html-5.0">

<script>
geoFindMe()
function geoFindMe() {
  var output = document.getElementById("out");

  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;
    var at = {!CTPHARMA__Activity__c.SF1_Attempts__c};        
    document.getElementById('page:form:lati').value = latitude.toString().replace(".",",")  ;
    document.getElementById('page:form:long').value = longitude.toString().replace(".",",")  ; 
    document.getElementById('page:form:SF1').checked = true;
    document.getElementById('page:form:Attempts').value  = (at + 1);
    callActionFunction()
   
  };

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  };

  navigator.geolocation.getCurrentPosition(success, error);
}
</script>

<script>
        var x = {!CTPHARMA__Activity__c.SF1_Request_geo__c};
        function callActionFunction(){

        if(x){ 
                    ActionFunctionName();
        }}


</script>



<apex:form id="form" >
  <div style="display:none;">
latitude :<apex:inputField id="lati"  value="{!CTPHARMA__Activity__c.SF1_Lati__c}" /> <br></br>
longitude :<apex:inputField id="long" value="{!CTPHARMA__Activity__c.SF1_Long__c}" /> <br></br>
SF1 : <apex:inputField id="SF1" value="{!CTPHARMA__Activity__c.SF1__c}" />
Attempts : <apex:inputField id="Attempts" value="{!CTPHARMA__Activity__c.SF1_Attempts__c}" />



<apex:commandButton action="{!save}"  value="Save"/></div>


<apex:actionFunction action="{!quicksave}" name="ActionFunctionName"/>

<img src="{! IF(CTPHARMA__Activity__c.Is_confirmed_territorially__c == true, 'https://emojipedia-us.s3.amazonaws.com/thumbs/120/apple/114/thumbs-up-sign_1f44d.png','https://emojipedia-us.s3.amazonaws.com/thumbs/120/apple/114/thumbs-down-sign_1f44e.png')}" align="center"/>   <br></br>
<apex:outputText value="{!CTPHARMA__Activity__c.SF1_Status__c}"/>

</apex:form>
</apex:page>

 
piyush_sonipiyush_soni
try below code :
<apex:page standardController="CTPHARMA__Activity__c" id="page" docType="html-5.0">

<apex:form id="form" >
  <div style="display:none;">
   latitude :<apex:inputField id="lati"  value="{!CTPHARMA__Activity__c.SF1_Lati__c}" /> <br></br>
   longitude :<apex:inputField id="long" value="{!CTPHARMA__Activity__c.SF1_Long__c}" /> <br></br>
   SF1 : <apex:inputField id="SF1" value="{!CTPHARMA__Activity__c.SF1__c}" />
   Attempts : <apex:inputField id="Attempts" value="{!CTPHARMA__Activity__c.SF1_Attempts__c}" />

<apex:commandButton action="{!save}"  value="Save"/></div>

<apex:actionFunction action="{!quicksave}" name="ActionFunctionName"/>

<img src="{! IF(CTPHARMA__Activity__c.Is_confirmed_territorially__c == true, 'https://emojipedia-us.s3.amazonaws.com/thumbs/120/apple/114/thumbs-up-sign_1f44d.png','https://emojipedia-us.s3.amazonaws.com/thumbs/120/apple/114/thumbs-down-sign_1f44e.png')}" align="center"/>   <br></br>
<apex:outputText value="{!CTPHARMA__Activity__c.SF1_Status__c}"/>

</apex:form>

<script>
geoFindMe();
function geoFindMe() {
  var output = document.getElementById("out");
  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;
    var at = {!CTPHARMA__Activity__c.SF1_Attempts__c};        
    document.getElementById('page:form:lati').value = latitude.toString().replace(".",",")  ;
    document.getElementById('page:form:long').value = longitude.toString().replace(".",",")  ; 
    document.getElementById('page:form:SF1').checked = true;
    document.getElementById('page:form:Attempts').value  = (at + 1);
    callActionFunction();
   
  };

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  };

  navigator.geolocation.getCurrentPosition(success, error);
 }
   var x = {!CTPHARMA__Activity__c.SF1_Request_geo__c};
   function callActionFunction(){
   if(x){ 
     ActionFunctionName();
   }
   }
</script>
</apex:page>
let me know if it helps you
thanks


 
This was selected as the best answer
AK_SmithAK_Smith

Thanks! Where to add your script?
<apex:page standardController="CTPHARMA__Activity__c" id="page">

<apex:form id="form" >

<apex:inputField id="A"  value="{!CTPHARMA__Activity__c.SF1_UA__c}" /> 

</apex:form>

<script>
agent();
function agent() {
    var u = window.navigator.userAgent;
    //  alert(u);
       document.getElementById('page:form:A').value  = u;
       }
</script>

</apex:page>