Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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>
share|improve this answer
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 Nov 5 '12 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 Nov 8 '12 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 Dec 18 '13 at 2:51

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! :-)

share|improve this answer
3  
caveat emptor, anchors are intrinsically client-side and aren't transported to the server - great of Salesforce to fix the PageReference's constructor though! –  user320 Jan 9 at 12:37

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
share|improve this answer
    
Yowzers. Great demonstration. I'm going to file this as a bug w/ SFDC. –  Benj Nov 8 '12 at 19:52
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()?

share|improve this answer
    
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 Nov 5 '12 at 23:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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