Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal: Creating anchor only link with l()

Tags:

drupal

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

like image 295
ack Avatar asked Nov 09 '09 00:11

ack


1 Answers

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.

like image 122
googletorp Avatar answered Sep 16 '22 19:09

googletorp