0

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);

    }
}
4

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.