Below is a VF Page I have. I do get the alert message with the URL. I am unable to get theValue variable value in Apex controller. Am I missing something here?
<apex:page standardController="Sobject__c" extensions="APage"> <script> var old = document.referrer; function setVal(){ document.getElementById("{!$Component.hdnField}").value = document.referrer; } alert(old); </script> <script> setVal(); </script> <apex:form > <apex:inputHidden id="hdnField" value="{!theValue}" /> </apex:form> <apex:pageMessages /> <apex:detail subject="{! Sobject.id}"/> </apex:page>
Controller:
public class APage { public String theValue{get;set;} public APage(ApexPages.StandardController controller) { system.debug('############ the value ################' + theValue); } }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can’t have submitted the value by the time your constructor is called; the page hasn’t loaded yet. However, you should be able to check the referrer from Apex Code this way:
public APage(ApexPages.StandardController controller) { String referrer = ApexPages.currentPage().getHeaders().get('REFERER'); }
Note that this is not a typo; the “referer” header was spelled wrong many years ago, so we’re stuck with this for backwards compatibility. Many browsers may choose to not set this header if you’re coming from a different domain, so it’s really only useful if you’re navigating between Visualforce pages.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0