2

I am trying to capture the URL of the previous page in a custom VF Page, when someone clicks on my custom tab from any page, especially when they are in Setup or Reports area. Following is what I am trying to do, which is not giving me the desired results. I’ll appreciate if anyone can provide any insights on this.

My goal is to capture the previous page url, execute my logic on my custom VF page, and redirect the user to the same previous page.

How to Capture Previous Page URL

I have a Custom Tab ‘Check URL’ that displays the custom VF Page. I want to display the following url in the case of click the tab from setup area:

https://na15.salesforce.com/setup/forcecomHomepage.apexp?setupid=ForceCom&retURL=%2Fhome%2FshowAllTabs.jsp

The code is as follow:

<apex:page standardStylesheets="true" sidebar="false" showHeader="false">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>

var j$ = jQuery.noConflict();
   j$(document).ready(function() {
    var oldURL = document.referrer;
    alert(oldURL);

});                           

 </script>

<h1>   {!$CurrentPage.URL}</h1>

       <p>
 <a href="javascript:history.go(-1)" title="Return to the previous page">&laquo; Go back</a>
</p>

</apex:page>

1 Answer 1

6

You can get the prior page using (Apex Code):

String referrer = apexpages.currentPage().getheaders().get('Referer');

This value may be unreliable if the user uses a bookmark or when Developer Mode is enabled.

2
  • @Faisal You're right. I didn't think about that (there is definitely a redirect that happens). Given that Referer is not going to work, you might have a problem here; since your page (Visualforce) is not on the same domain as the origin (platform/instance), you can't access that history item, as a matter of cross-scripting rules built into browsers (imagine if Big Evil Company could read your entire history to learn what pages you've viewed "for marketing purposes"). You might not have any viable solution beyond history.go(-1).
    – sfdcfox
    Commented Jan 17, 2014 at 17:03
  • Yes, @sfdcfox is correct, if the developer mode is enabled, the referer header always points to the current page. Commented Dec 15, 2016 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .