Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spot The Error In This Little Javascript

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
The following is in the head of an html document:

<script language=&quot;javascript&quot;>
function chg_box('item_name', switch)
{ {
item_name.style.backgroundColor = '999999';
}
if(switch == 1)

else
{
item_name.style.backgroundColor = '993333';
}
}

</script>



In the body of the html document I have this:

<div class=&quot;item&quot; id=&quot;Gold River&quot; onmouseover=&quot;chg_box('Gold River', 1);&quot; onmouseout=&quot;chg_box('Gold River', 0);&quot;>Gold River</div>



When I open the page I get an error
line 12 character 21 identifier expected
which corresponds to the first ' in the function definition in the head of the document.

If I mouseover or mouseout on the div I get this error
line 30 character 1 object expected
which corresponds to the beginning of the line with the div on it.

Please let me know if you have any suggestions.
 
For one thing, you don't want the tic marks in the function header, just function chg_box(item_name, switch). Then you need to use eval to get the variable into the statement:
Code:
eval(item_name+&quot;.style.backgroundColor&quot;) = '999999';
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
ok, I've updated the javascript so that it's now like this:

<script language=&quot;javascript&quot;>
function chg_box(item_name, switch)
{
if(switch == 1)
{
eval(item_name+&quot;.style.backgroundColor&quot;) = '999999';
window.status = item_name;
}
else
{
eval(item_name+&quot;.style.backgroundColor&quot;) = '993333';
window.status = '';
}
}
</script>


In the body of my html, I have this:

<div class=&quot;item&quot; id=&quot;Gold River&quot; onmouseover=&quot;chg_box(Gold River, 1);&quot; onmouseout=&quot;chg_box(Gold River, 0);&quot;>Gold River</div>

It's still not working though. Is there something wrong with the way I am passing the two variables to the function?
 
must be smth like this:
<div class=&quot;item&quot; id=&quot;Gold River&quot; onmouseover=&quot;chg_box('Gold River', 1);&quot; onmouseout=&quot;chg_box('Gold River', 0);&quot;>Gold River</div>


but w/o ' in ur script..

that way.. :cool:
regards, vic
 
I just made the change that vituz suggested and I am still getting errors.
I get an error when the page loads and another error whenever I mouseover or mouseout on the div.

Thanks for the help
 
* if you get an error when the page loads isn't that you're calling a function when the page loads ?
* also, i don't think even ie can find item_name alone, if you don't help a bit :
function chg_box(item_name, switch) {
if(switch == 1)
{
eval(&quot;document.all.&quot;+item_name+&quot;.style.backgroundColor&quot;) = '999999';
...
(this is ie only)

* if it's still not working, try this :
function chg_box(item_name, switch)
{
alert(&quot;i received : &quot;+item_name+&quot; and &quot;+switch)
my_obj = eval(&quot;document.all.&quot;+item_name)
alert(&quot;does it write 'object' ?&quot; + my_obj)
alert(&quot;is it still ok ?&quot; + my_obj.style)
alert(&quot;and now ?&quot; + my_obj.style.backgroundColor)
alert(&quot;oh and maybe it was window.status ??&quot; + window.status)
}
the 1st one popping with &quot;undefined&quot; or not popping is the culprit
------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
I have added the document.all part and it still won't work.

I get an error when the page loads due to some problem with the javascript function in the header.

Here's the function again:
<script language=&quot;javascript&quot;>
function chg_box(item_name, switch)
{
alert(&quot;i received : &quot;+item_name+&quot; and &quot;+switch);
if(switch == 1)
{
eval(&quot;document.all.&quot;+item_name+&quot;.style.backgroundColor&quot;) = '999999';
window.status = item_name;
}
else
{
eval(&quot;document.all.&quot;+item_name+&quot;.style.backgroundColor&quot;) = '993333';
window.status = '';
}
}
</script>
 
WHAT error are you getting ?
what do all the alert say ? ------
please review FAQ183-874 - this will help you to get the best out of tt
[ &quot;you&quot; is not someone in particular - don't take it too personnal ]
 
The Alert never happens because there is some error in the function.

The 2 errors I get are &quot;expected identifier&quot; from the javascript function and &quot;object expected&quot; when I mouseover or mouseout of the div.
 
I don't know about Javascript, but in C/C++ switch is a reserved word. Try changing all instances of that variable to say &quot;switch1&quot; or something like that.

Hope that helps,
Segfault
 
Segfault7375 was correct....it's a reserved word.

I'm getting a new error now: &quot;cannot assign to a function result&quot;

This happens whenever I mouseover or mouseout and relates to the following:
if(stat == 1)
{
eval(&quot;document.all.&quot;+item_name+&quot;.style.backgroundColor&quot;) = '999999';
window.status = item_name;
}
else
{
eval(&quot;document.all.&quot;+item_name+&quot;.style.backgroundColor&quot;) = '993333';
window.status = '';
}


Thanks for the help
 
Try it like this:

Code:
if(stat == 1)
{
   eval(&quot;document.all.&quot; + item_name + &quot;.style.backgroundColor = #999999&quot;);
   window.status = item_name;
}
else
{
   eval(&quot;document.all.&quot; + item_name + &quot;.style.backgroundColor&quot; = #993333&quot;);
   window.status = '';
}

That may work for you (I don't have my tools with me at the moment)

Hope that helps,
Segfault
 
I just put in both of those changes and I am still getting the same error.

I'm sure it's something simple but I can't see it.
 
Try this:
Code:
document.all(item_name).style.backgroundColor = &quot;#99999&quot;;
You may not even need the eval for this.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I think I would have a better chance of figuring this out if I knew what eval did....

Can someone give me an explanation?
 
I've read up on eval a bit. If I do eval(&quot;document.all.&quot; + item_name + &quot;.style.backgroundColor&quot;);
what does that do?

does it return the current background color, or does it return a pointer to the memory location that holds the background color?

I remembered hearing something about a function called getElementById(); would that be useful here?

thanks again.
 
As I understand it, eval take a character string and evaluates it as a command (or part of a command).

Did you try what I suggested above (without the eval)? I.E.:
Code:
document.all(item_name).style.backgroundColor = &quot;#99999&quot;;
That's the syntax I used to make it work (IE only), except I had some extra stuff for identifying a specific table row and cell as well.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
hie its me again..
Dave, what browser do u use? regards, vic
 
I am using IE 4...long story, but 5.5 totally screws my machine. I have tested the page on 5.5 and it does exactly the same thing.

I'm just going to paste the whole html file in here so people can run it and see.

Before you laugh at the UGLY colors and terrible layout, keep in mind that this is a test I am doing to see if I can get the javascript to work..

here it is:

<html>
<head>
<title>Jason's Thingy</title>
<style type=&quot;text/css&quot;>
.item {font-family: sans-serif; color: #ffffff; font-style: normal; border-style: solid; border-color: #ff0000; border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px; padding-bottom: 1px; margin-left: 1px; margin-right: 1px; margin-top: 1px; margin-bottom: 1px; background-color: #000000;}
a:active {text-decoration: none;}
a:hover {text-decoration: none;}
a:link {text-decoration: none;}
a:visited {text-decoration: none;}
</style>
<script language=&quot;javascript&quot;>
function chg_box(item_name, stat)
{
temp = (eval(&quot;document.all.&quot; + item_name + &quot;.style.backgroundColor&quot;));
if(stat == 1)
{
temp = '#999999';
window.status = item_name;
}
else
{
temp = '#993333';
window.status = '';
}
}
</script>
</head>
<body bgcolor=&quot;#ffffff&quot;>

<img src=&quot;map.gif&quot; width=&quot;800&quot; height=&quot;524&quot;><br>
<div class=&quot;item&quot; onmouseover=&quot;this.style.backgroundColor='ff66cc'; window.status='Coal Harbour'; return true;&quot; onmouseout=&quot;this.style.backgroundColor='336633'; window.status=''; return true;&quot;><a href=&quot;#&quot;>Coal Harbour</a></div>
<div class=&quot;item&quot; id=&quot;Gold River&quot; onmouseover=&quot;chg_box('Gold River', 1);&quot; onmouseout=&quot;chg_box('Gold River', 0);&quot;>Gold River</div>
<div class=&quot;item&quot;>Harrison Lake</div>
<div class=&quot;item&quot;>Haslam Lake</div>
<div class=&quot;item&quot;>Head Bay</div>
<div class=&quot;item&quot;>Holberg</div>
<div class=&quot;item&quot;>Houston</div>
<div class=&quot;item&quot;>Ingram</div>
<div class=&quot;item&quot;>Jeune Landing</div>
<div class=&quot;item&quot;>Jordan River</div>
<div class=&quot;item&quot;>Kendrick Inlet</div>
<div class=&quot;item&quot;>Kimsquit</div>
<div class=&quot;item&quot;>Koprino</div>
<div class=&quot;item&quot;>MacNair</div>
<div class=&quot;item&quot;>Naka Creek</div>
<div class=&quot;item&quot;>Nootka</div>
<div class=&quot;item&quot;>Oweekeno</div>
<div class=&quot;item&quot;>Pemberton</div>
<div class=&quot;item&quot;>Plumper Harbour</div>
<div class=&quot;item&quot;>Port Eliza</div>
<div class=&quot;item&quot;>Port McNeill</div>
<div class=&quot;item&quot;>Roderick Island</div>
<div class=&quot;item&quot;>Saanich Forestry Centre</div>
<div class=&quot;item&quot;>SewellInlet</div>
<div class=&quot;item&quot;>Squamish</div>
<div class=&quot;item&quot;>Stafford Lake</div>
<div class=&quot;item&quot;>Swanson Bay Heli Log</div>
<div class=&quot;item&quot;>Tahsis</div>
<div class=&quot;item&quot;>Theodosia Inlet</div>
<div class=&quot;item&quot;>Winter Harbour</div>
<div class=&quot;item&quot;>Yeo Island</div>
<div class=&quot;item&quot;>Zeballos</div>

</body>
</html>
 
well, looks like i got it to work:
<script language=&quot;javascript&quot;>
function chg_box(item_name, stat) {
if(stat == 1) {
eval(&quot;document.all.&quot; + item_name + &quot;.style.backgroundColor='#999999'&quot;)
window.status = item_name; }
else {
eval(&quot;document.all.&quot; + item_name + &quot;.style.backgroundColor='#993333'&quot;)
window.status = ''; }
}
</script>
..
<div class=&quot;item&quot; id=&quot;Gold_River&quot; onmouseover=&quot;chg_box('Gold_River', 1);&quot; onmouseout=&quot;chg_box('Gold_River', 0);&quot;>Gold River</div>
note:
never ever use names/ids with spaces!! regards, vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top