...

View Full Version : function doesn't always return a value ... but I can't find a safe value to return



brothercake
03-14-2004, 04:11 AM
I've got an anonymous function attached to a link, which might be activated via onclick or onmouseover - in both cases the handler opens a submenu, but if it's onclick it should return false so the link isn't followed.

Essentially:


linkObject[eventHandler] = function(e)
{
this.openMenu(e.target);

if(eventHandler == 'onclick')
{
return false;
}

}

Fine, but that generates a strict warning - "anonymous function does not always return a value".

Trouble is ... I can't return anything else except false, because then the link HREF doesn't show up in the status bar in Internet Explorer when you hover over it!

And I can't universally return false either, because events from links in nested menus won't work anymore - they'll bubble up and return false - so you won't be able to click them.

So what can I do? I just wanna get rid of that strict warning, so I guess I either need another way of phrasing that anonymous function, or I need a solution to why any other return value but false makes the links not show up in the status bar in IE.

liorean
03-14-2004, 10:50 AM
The default return from any function that has no explicit return statement is undefined. So, including a clean return; at the end of the function should do it.

As for bubbling, have you considered returning false on the top level in the menu, or returning false but explicitly triggering the event on the parent node?

[edit:]the first of which does not work, the second of which is not needed since returning false does not cancel the cascade.

brothercake
03-15-2004, 01:50 AM
Follow-up for reference - with liorean's help we got this issue nailed down.

IE was not showing the mouseover status text with any return value but false, but if it's always returning false then the links in nested menus don't work.

Then to add a spanner in the works, Opera 7 and MSN have the opposite problem from IE - the status text only shows if the mouseover returns true.

So the solution was to find the event-type - click or mouseover - and then return true or false depending on the event and browser. Like this:


linkObject[eventHandler] = function(e)
{
this.openMenu(e.target);

if(eventHandler == "onclick")
{
return false;
}

return (e.type == "click" || isOP7 || isMSN);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum