{"id":6736,"date":"2012-08-28T15:04:55","date_gmt":"2012-08-28T09:34:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6736"},"modified":"2016-12-19T15:16:41","modified_gmt":"2016-12-19T09:46:41","slug":"appending-anchor-tag-to-the-url-on-server-side-using-fragment-parameter-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/appending-anchor-tag-to-the-url-on-server-side-using-fragment-parameter-in-grails\/","title":{"rendered":"Appending anchor tag to the url on server side using fragment parameter in Grails"},"content":{"rendered":"<p>Hi,<br \/>\n<br \/>\nRecently I had come across one of the cool grails <a href=\"http:\/\/grails.org\/doc\/2.1.0\/ref\/Controllers\/redirect.html\">redirect<\/a> parameter named <strong>&#8216;fragment&#8217;<\/strong> providing cool feature of forming relative link with an anchor tag\/name.<\/p>\n<p>\n<br \/>\nGenerally, 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&#8217;s anchor name is specified by you on HTML element as attribute id.\n<\/p>\n<p>&nbsp;<br \/>\n&#8220;<strong>anchorName<\/strong> specified here in url will specify the initial focus on the content whose anchor name is specified in url after loading of the page&#8221;<br \/>\n&nbsp;<br \/>\n<strong>For Example :<\/strong><br \/>\nSuppose 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.<br \/>\n&nbsp;<br \/>\n<em><strong>To generate the url with anchor name on server side, we use grails redirect parameter named &#8216;fragment&#8217;.<\/strong><\/em><br \/>\n<br \/>\nDefined as in Grails Docs for <a href=\"http:\/\/grails.org\/doc\/2.1.0\/ref\/Controllers\/redirect.html\">redirect<\/a> , Fragment : The link fragment (often called anchor tag) to use.<br \/>\n<br \/>\nwe simply need to write in our grails controller : [groovy]redirect(controller:&#8217;product&#8217;,action:&#8217;list&#8217;,fragment:&#8217;coffee&#8217;);[\/groovy] It will form the url <strong><\/strong>. 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.<br \/>\n<br \/>\nI hope it will help you in solving your use cases related to it \ud83d\ude42<br \/>\n<br \/>\nThanks,<br \/>\nTarun Pareek<br \/>\ntarun@intelligrape.com<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/author\/tarun\/\">More Blogs by Me<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, Recently I had come across one of the cool grails redirect parameter named &#8216;fragment&#8217; 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&#8217;s anchor name [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[898,4840,4842],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6736"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6736"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6736\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}