function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mulvelingmulveling 

Is it possible to access the current URL's anchor string in custom controller code?

The application I'm working on involves a large number of pages (many of which update dynamically via AJAX actions) mapped to a small set of controllers. Currently, most combinations of page & state are not bookmark-able. The platform seems to take control of the location in the address bar when you're forwarding to another page of the same controller - query parameters are dropped, and the displayed page name often lags behind what's actually active. Creating a bookmark at such a time will result in an error when you try to load the bookmark - either for required query parameters missing, or the requested page being wrong for the active object's current state. Then, there's the whole issue of bookmarking state that was the result of an AJAX action...

 

One typical solution for this is to use Javascript to load the necessary state into the anchor portion of the address bar's location - that is, the part following the leading '#' char (this does not cause the browser to re-load the page, which is why it's useful). The anchor is saved as part of the bookmark, and then when it's loaded, the application logic can use it to re-instantiate the correct state.

 

That brings us to Visualforce. To fully implement this technique, we need a way to read the anchor string from the controller code so that we can instantiate the correct state, forward to the correct subpage, etc. The PageReference's "getAnchor()" method looked promising, but when I tried it out in a test page "/apex/EchoAnchorTest?foo=bar#foobar" - the call "ApexPages.currentPage().getAnchor()" and "System.currentPageReference().getAnchor()" always returned null. I'm really REALLY hoping for a way to access the "#" part of the current URL in Apex. Is there something I'm missing, or has anyone had luck finding an effective workaround?

 

Mike

Abhishek_DEOAbhishek_DEO
Perhaps getAnchor() works only when we set anchor using setAnchor method. I did not test this but there is an IDEA posted here (https://success.salesforce.com/ideaView?id=08730000000a8R1AAI

You may try accessing anchor using JS code and pass that(using <apex:param>) to controller method using actionfunction.

You may look into below code just to get an idea about JS implementtaion
 
<apex:page>
<script type="text/javascript" >
 window.onload = function(){

     var showanchor = window.location.hash.substring(1);
     alert(showanchor) 
 };
 </script>
</apex:page>

Please mark this as suitable answer if it helps you.