Forum Moderators: open
I've got a rollover menu which until now has been fine, but I've got a new link to the site search engine. I want this engine to reside in a directory 'search', but if I try to pass an object called 'search' to a javascript function then it does not, and object.name etc are declared undefined. I might have expained badly so here is a bit of code...
This is the Javascript rollover on function:
--------------------------------
function on(obj)
{
if (document.images)
{
obj.src = path + obj.name + '_on.gif';
}
}
--------------------------------
This is a stripped down example of a link that works:
--------------------------------
<a href="link" onMouseOver="Javascript:on(history)">
<img src="history_off.gif" name="history"><a>
--------------------------------
This is the link that doesn't:
--------------------------------
<a href="link" onMouseOver="Javascript:on(search)">
<img src="search_off.gif" name="search"><a>
--------------------------------
Any ideas? I really would like to name this image 'search' for various reasons. Is the word search a special case in Javascript, I can't seem to find it anywhere...
You really couldn't have picked worse names. Both 'history' and 'search' are part of the pre-defined JS object model.
The following passes as strings and uses eval to combine the strings:
function on(obj)
{
if (document.images)
{
eval(obj+".src = path + "+obj+".name + '_on.gif'");
}
}
___________<a href="link" onMouseOver="Javascript:on('history')">
<img src="images\history_off.gif" name="history"><a>
¦
<a href="link" onMouseOver="Javascript:on('search')">
<img src="images\search_off.gif" name="search"><a>
and it works for search but not history!!! Can you not call it find instead?
function on(obj)
{
if (document.images)
{
if (typeof obj == 'object')
{
obj.src = path + obj.name + '_on.gif';
}
else
{
eval(obj+".src = path +"+obj+".name + '_on.gif'");
}
}
}
</script>
______________________
<a href="link" onMouseOver="Javascript:on(history)">
<img src="history_off.gif" name="history"><a>
<a href="link" onMouseOver="Javascript:on('search')">
<img src="search_off.gif" name="search"><a>
Does anyone have a way around this without fudging - i'd love to hear.
Josh
You can use this if you are willing to pass all the object names as strings (i.e. in '').
I won't be beaten by this!!
function on(obj)
{
if (document.images)
{
document.images[obj].src = path + obj + '_on.gif';
}
}
____________________<a href="link" onMouseOver="Javascript:on('history')">
<img src="images\history_off.gif" name="history"><a>
<a href="link" onMouseOver="Javascript:on('search')">
<img src="images\search_off.gif" name="search"><a>
To allow your initial code to work you could pass the explicit image object like so:
<a href="link" onMouseOver="Javascript:on(document.images.search)">
You'd only need to do this for the troublesome ones.
I think I've had enough of this now. Hope that helps -- it's certainly helped me sort some things out in my head.