Here is the theJavascript to disable a menu item at run time

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
powlette
Beginner
Beginner
Posts: 2
Joined: Wed Oct 15, 2003 3:07 pm
Contact:

Here is the theJavascript to disable a menu item at run time

Post by powlette »

If you need to disable menu item, gray it out, and disbale the url you can use this script. It can also restore the url and color.

call it like this:

setMenuItemEnabled("Main Menu", 1, false);

Code: Select all

function setMenuItemEnabled(menuname, itemNumber, enabled)
{
	if (!document.all) return;
	vals = (""+_m[getMenuByName(menuname)]).split(",");
	var item = _mi[parseInt(vals[0])+itemNumber];
	indexName = 1;	indexURL = 2;	indexOffColor = 6;	indexOnColor = 8;	indexBackupURL = 20;
	if (item[indexBackupURL] == undefined) item[indexBackupURL] = item[indexURL];
	if (enabled)
	{
		item[indexURL] = item[indexBackupURL];	item[indexOffColor] = '000000';	item[indexOnColor] = '000000';
	}
	else
	{
		item[indexURL] = 'javascript:';	item[indexOffColor] = '999999';	item[indexOnColor] = '999999';
	}
	_popi(parseInt(vals[0])+itemNumber); 
}
If you enhance this or find it useful, please let me know
jody@powlette.com
Jody F Powlette
schalkley
Beginner
Beginner
Posts: 1
Joined: Fri Sep 10, 2004 9:46 am
Location: London, UK

Amended version of dynamically enabling/disabling menu

Post by schalkley »

Jody,

Your function was a great help, but I found it only disabled the top level menus and any submenus still appeared. I found here in the forums that you can disable a menu using
aI("url=http://www.nba.com;type=disabled;");
but I want to do this dynamically.
I found the index number of the 'type' which is 34, and amended your function as follows.

// disable or enable the Milonic menu.
// @param disable - true to disable, false to enable
function disableMilonicMenu(disable) {
// this is the name from the menu_data.js
var mainMenuName="Main Menu";

// this is the number of top level menu items on the menu not including
// the 'Milonic' that appears in the unlicensed version of the menu.
var numberOfTopLevelMenus = 8;

if (!document.all) return;
indexType=34; indexBackupURL = 20; indexURL = 2;
vals = (""+_m[getMenuByName(mainMenuName)]).split(",");
for (itemNumber=1; itemNumber < numberOfTopLevelMenus+1; itemNumber++)
{
var item = _mi[parseInt(vals[0])+itemNumber];

if (item[indexBackupURL] == undefined) item[indexBackupURL] = item[indexURL];
if (disable)
{
item[indexType] = 'disabled';
}
else
{
item[indexType] = 'enabled';
}
_popi(parseInt(vals[0])+itemNumber);
}
}

This disables or enables the top level menus (change the two values in the function to suit your site).
There is a bug though. The last menu item is changed to the colour of highlighted.
Anyone fix this?

Thanks
Stephen Chalkley.
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hi Stephen,

You might also take a look at a function called mm_changeItemProperty(), described in detail in this thread. You can use the function to change any property in any menu item dynamically, including setting the type property (34 as you found) to disable and re-enable a menu item.

Hope that helps,

Kevin
Post Reply