Appending anchor tag to the url on server side using fragment parameter in Grails

28 / Aug / 2012 by Tarun Pareek 0 comments

Hi,

Recently I had come across one of the cool grails redirect parameter named ‘fragment’ providing cool feature of forming relative link with an anchor tag/name.


Generally, we can simply call [groovy]window.location.href=http://yourLink#anchorName[/groovy] It will focus you to the anchor name specified in relative link means scroll the page to the anchored tag who’s anchor name is specified by you on HTML element as attribute id.

 
anchorName specified here in url will specify the initial focus on the content whose anchor name is specified in url after loading of the page”
 
For Example :
Suppose we have three divs with id tea, colddrink and coffee. When this page load with its url ending with hash tag #coffee, it will focus on the page content containing the div with id coffee . i.e Page will be scrolled to the content where div with id coffee is found, while loading the page.
 
To generate the url with anchor name on server side, we use grails redirect parameter named ‘fragment’.

Defined as in Grails Docs for redirect , Fragment : The link fragment (often called anchor tag) to use.

we simply need to write in our grails controller : [groovy]redirect(controller:’product’,action:’list’,fragment:’coffee’);[/groovy] It will form the url . When the view is being rendered and while loading of HTML, page will be scrolled to the content where div with id coffee is found.

I hope it will help you in solving your use cases related to it 🙂

Thanks,
Tarun Pareek
tarun@intelligrape.com

More Blogs by Me

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *