Drupal dhtml_menu accommodate non-javascript viewers

I wanted to find a solution for using dhtml menus while accommodating non-javascript viewers.
Turns out it was an easy fix.

The menu currently uses drupal’s: drupal_add_css to append the css document which collapses all the menu items by default. Since the menu module relies on jQuery, I changed the append method to use jQuery instead.
Here’s my suggested fix:
Line: 20 in dhtml_menu.module;

function dhtml_menu_init() {
+ //append the css with javascript instead of php to accommodate non-javascript users by displaying menu items by default.
- drupal_add_css(drupal_get_path('module', 'dhtml_menu') .'/dhtml_menu.css');
+ drupal_add_js("$('head').append('');", "inline");

I’ve tested it with success on my Mac: OS 10.5.8: with FireFox 3.5.3, Safari 4.0.3 and Opera 9.64
PC: Window’s 7: IE8 and FF 3.0.14

I think it will help other people as well so I posted my fix here:
http://drupal.org/node/616026#comment-2197294

I hope they incorporate it into the module so I don’t have to maintain my own version.

Updated suggestion 11-9-09;
I encountered a situation where it’s better to refer to the root in the head.append so I added a slash before drupal_get_path.
Here’s the updated code relative to the current release:

function dhtml_menu_init() {
+ //append the css with javascript instead of php to accommodate non-javascript users by displaying menu items by default.
- drupal_add_css(drupal_get_path('module', 'dhtml_menu') .'/dhtml_menu.css');
+ drupal_add_js("$('head').append('');", "inline");
Leave a Comment