I'd like to output this
<a href='#namedanchor'>link</a>
using the l() function, so that the link just jumps to an anchor on the current page.
I expected this to work
l('link', '', array('fragment' => 'namedanchor'));
but it creates an absolute link to www.example.com/#namedanchor
instead of www.example.com/currentpage#namedanchor
If you want to create a link with just the fragment, you need to "trick" the url function a bit. As it will append the basepath to all internal urls, ''
will become http://example.com
.
What you need to do is to set the external option to true:
l('link', '', array('fragment' => 'namedanchor', 'external' => TRUE));
This will give the desired
<a href='#namedanchor'>link</a>
Alternative you could give the full url like Jeremy suggests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With