Since v4 at least, Drupal couldn't handle links to named anchors in the current HTML document, e.g., a menu item "top" whose path is "#top" doesn't work properly.

I've been using a simple patch since 5.x that has worked without causing any other problems.

I can create a menu item "top" whose path is "#top" by adding the following code (line numbers are in 6.0beta3):

In common.inc, line 1230 in function 'url(...)', add line:

if(strpos($path, '#') === 0) return $path

In menu.inc, line 2152 in function 'menu_path_is_external($path)', add line:

if(strpos($path, '#') === 0) return $path;

I'm sure you could do some more checking if you want, but if it's a named anchor in the current document, I don't think anything strange after the leading '#' will cause any problems in HTML.

I hope this helps,

Mitch

Comments

chx’s picture

Status: Active » Fixed

menu_link_save deals with options already. The menu UI allows you to enter fragments. Router definitions can't use fragments but you can do a menu_link_save.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

amfis’s picture

Version: 6.0-beta3 » 6.20
Category: bug » support
Status: Closed (fixed) » Needs review

And yet.. I don't see an option in menu UI to enter anchor (fragment) nor there are explanations on how to do it.

amfis’s picture

Right.. looks like I've found how to do it finally!
I need to use internal path like node/6#named_anchor to make it work.

So is it just me, or that explanation MUST be entered to a menu 'add item' UI description field?

sorensong’s picture

Issue tags: +anchor, +main menu, +anchor fragment

@amfis have you been able to add simply "#1" though? For what i'm doing I need to be able to add an anchor fragment and haven't found a solution. Frustrating.

amfis’s picture

Yes it is :)

Your solution would look like this: l('link title', 'path', array('fragment' => '1')); And if you want to use UI, input system path not alias, like this: node/10#1

Just in case anyone wanted to know how to do it, read: http://api.drupal.org/api/drupal/includes--common.inc/function/l/6, which states, you need to look at: http://api.drupal.org/api/drupal/includes--common.inc/function/url/6 :)

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.