homepage Welcome to WebmasterWorld Guest from 54.205.119.163
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript "search"
is this a reserved word/function?
rpking




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

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




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

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




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

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




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

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




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

[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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved