Welcome to WebmasterWorld Guest from 54.234.153.186

Forum Moderators: open

Message Too Old, No Replies

Javascript "search"

is this a reserved word/function?

     

rpking

3:40 pm on Jul 11, 2001 (gmt 0)

10+ Year Member



I'm making a site that rebuilds itself every night to allow static pages to keep fresh. Much of the addresses and navigation relies on clear naming of variables etc, which are later used as directory names and headers.

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...

joshie76

4:06 pm on Jul 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In JS the window.location.search returns the querystring at the end of a URL (?querystring)..

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?

joshie76

4:21 pm on Jul 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bit of a fudge but as long as you pass the search in the function with '' then this'll work.

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

joshie76

4:26 pm on Jul 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ha ha,

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>

joshie76

4:31 pm on Jul 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[big]Me again!![/big]

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.

rpking

11:45 am on Jul 12, 2001 (gmt 0)

10+ Year Member



Fantastic! Cheers joshie... not 1, not 2 but 4 solutions. I'd fudged it myself with a bit of a modification in the 'on' function. I think I'll use your 3rd effort for now.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month