Using named anchors with menus
Users who have tried using “named anchors,” also known as “jump links,” in the menu system can run into problems with them not to working. For clarification, a named anchor looks like this:
<a name="anchor"></a>
The expected behavior is that when a link that points to this named anchor is clicked, the user’s browswer should jump to a to the anchor tag on the html page, making it appear at the top of the user’s browser window.
This desired behavior can be accomplished by adding the following function to your theme’s template.php file:
function phptemplate_menu_item_link($item, $link_item) {
// Convert anchors in path to proper fragment
$path = explode('#', $link_item['path'], 2);
$fragment = !empty($path[1]) ? $path[1] : NULL;
$path = $path[0];
return l(
$item['title'],
$path,
!empty($item['description']) ? array('title' => $item['description']) : array(),
!empty($item['query']) ? $item['query'] : NULL,
$fragment,
FALSE,
FALSE
);
}
Code not necessary
As drupal outputs strict xhtml 1.1 named anchors ie.
<a name="anchor"></a>do not work.They have been deprecated in xhtml 1.1 and replaced with the id tag. The id can be used on any tag eg.
<p id="anchor"></p>and then the anchor can be called as you would previously ie.To get the links to the anchors working in your menu you need the full url as the link ie.
not
The above code is not necessary for this to work.
good point about id's but.....
I had forgotten that named anchors were deprecated, so, thanks for reminding me. Much cleaner anyhow.
However, absolute urls are not viable for a production site. The whole point of a framework (and cms in drupal's case) is to abstract as much as possible in order to make the application portable and scalable. I agree with others that a permanent solution for named anchors in Drupal 5.x menus would be greatly appreciated. Until then, I will try this template override.
thanks,
Brian
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://www.terraeclipse.com -- where I work
http://www.brianthomaslink -- where I will someday get around to playing