9

If I navigate to the following Visualforce URL:

https://cs10.salesforce.com/apex/TestArea#Anchor

... I'd like to be able to isolate the Anchor value via Apex. I've tried both of these approaches, which the documentation suggests should work:

  • Using System.URL: URL.getCurrentRequestUrl().getRef()
  • Using ApexPages: ApexPages.CurrentPage().getAnchor()

These both return blank/null values. What am I missing, and how do I get the anchor from the URL?

4 Answers 4

3

Assuming, that the anchor is not available directly in the controller, you could process the URL in an onLoad function and then pass the value to the controller either via an actionFunction or using Javascript Remoting

<apex:page>
<script type="text/javascript" >
 window.onload = function(){

     var achorElement = window.location.hash.substring(1);
     setAnchor(achorElement); //invoke action function
 };
 </script>

 <apex:form>
 <apex:actionFunction name="setAnchor" >
    <apex:param assignTo="{!anchorElement}" value="" /> //pass parameter to controller
 </apex:actionFunction>
 </apex:form>

</apex:page>
3
  • 1
    This could be a last-resort option. I'd like to avoid JS if at all possible because I'm planning to render the page differently depending on the anchor value, and would like to get the rendering right in the first place.
    – Benj
    Commented Nov 5, 2012 at 23:28
  • I'm accepting this as the best available work-around, since Ralph demonstrated that this isn't currently possible in Apex. Hopefully they'll fix this, though!
    – Benj
    Commented Nov 8, 2012 at 20:01
  • Worth clarifying that browsers don't send this hash to the server at all - so this isn't a limitation of Apex, it's a limitation of browsers and http!
    – Benj
    Commented Dec 18, 2013 at 2:51
5

It looks like from Spring'14 onwards you will be able to handle this in PageReference....

PageReference URIs Support Anchors. A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values. You can construct PageReference objects with URIs that include anchors or fragment identifiers, that is, an identifier following a hash symbol (#).

Nice! :-)

1
  • 4
    caveat emptor, anchors are intrinsically client-side and aren't transported to the server - great of Salesforce to fix the PageReference's constructor though! Commented Jan 9, 2014 at 12:37
2

It's not an answer, but I think javascript is going to be your only option. If that's a deal breaker switch from using the anchor as a parameter.

The only access you have to the url is via the ApexPages.currentPage().getUrl() or Url.getCurrentRequestUrl() and both have pre-processing that strips out the anchor. It's not documented, but it can be a pain in the ass if you're trying to redirect to page with an anchor.

For example:

String url = '/anotherPage?param=value#anchor';
PageReference pr = new PageReference(url);
system.assertEquals(url, pr.getUrl());

Results in:

Assertion Failed: Expected: /anotherPage?param=value#anchor, Actual: /anotherPage?param=value
1
  • Yowzers. Great demonstration. I'm going to file this as a bug w/ SFDC.
    – Benj
    Commented Nov 8, 2012 at 19:52
0
String link = 'https://cs10.salesforce.com/apex/TestArea#Anchor';
URL u = new System.URL(link);

System.debug(u.getRef()); // works for me, outputs "Anchor"

So I think you'll have to experiment with what kind of source string these methods return. Play with toExternalForm()?

1
  • It works if I construct a URL myself with a full URL, but not if I try to glean the anchor from the page URL. In other words, if I knew your link variable, I wouldn't need this!
    – Benj
    Commented Nov 5, 2012 at 23:26

You must log in to answer this question.

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