Herlp with Firefox

K

Krij

Hi!

Here's a training case at school. The function will animate a table
cell in IE:

function chgColor(){
var thistag, parenttag;
thistag = window.event.srcElement.tagName;
if(thistag == "TD"){
document.all(window.event.srcElement.sourceIndex).bgColor = "#3280ff";

Anybody that can give me the change I need for this to work in Firefox?

Thank you :)
 
R

Richard Cornford

Krij said:
Here's a training case at school. The function will animate
a table cell in IE:

function chgColor(){
var thistag, parenttag;
thistag = window.event.srcElement.tagName;
if(thistag == "TD"){
document.all(window.event.srcElement.sourceIndex).bgColor = "#3280ff";

Anybody that can give me the change I need for this to work in
Firefox?

Not without seeing how this function is being called.

Richard.
 
K

Krij

Sorry, here's the whole thing:

<html><head><title>JavaScript lessons Work Example</title>
<script language="javascript">
<!--
function chgColor(){
var thistag;
thistag = window.event.srcElement.tagName;
if(thistag == "TD"){
document.all(window.event.srcElement.sourceIndex).bgColor = "#3280ff";
}
}

function chgBack(){
var thistag;
thistag = window.event.srcElement.tagName;
if(thistag == "TD"){
document.all(window.event.srcElement.sourceIndex).bgColor = "#c0c0c0";
}
}
//--></script></head>
<body onmouseover="chgColor()" onmouseout="chgBack()" font
face="verdana" size="3" COLOR="#000000">
<center>
<table border="5" bordercolor="#006400" width="30%">
<tr>
<td>MAY</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td><td>NOT</td><td>&nbsp;</td><td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td><td>&nbsp;</td><td>WORK</td><td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>IN</td>
</tr>
<tr>
<td>FIREFOX</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr></table>

Thanks for answering :)


Richard Cornford skrev:
 
R

Richard Cornford

Krij wrote:
<script language="javascript">

The language attribute is deprecated and the type attribute required in
valid HTML 4, and its use makes the language attribute redundant:-


This 'hide from older browsers' stuff with HTML comment-like structures
is no longer necessary and should not be used.
function chgColor(){
^
Add a formal parameter so that the event can be passed to this
function:-

function chgColor(ev){

var thistag;
thistag = window.event.srcElement.tagName;
if(thistag == "TD"){
document.all(window.event.srcElement.sourceIndex).bgColor = "#3280ff";
}
}

As the event will be passed to the function as an argument there is no
need to reference IE's global event object. Non-IE browsers use the -
target - property to indicate the originator of the event, so:-

var theTag = ev.srcElement || ev.target;
var theTagName = theTag.tagName;
if(theTagName == 'TD'){
theTag.style.backgroundColor = '#3280ff';
}

- but the target may be a text node contained within an TD element so it
might be necessary to chain up the parent nodes until an element node is
encountered:-

function chgColor(ev){
var theTagName , theTag = ev.srcElement || ev.target;
while(theTag && theTag.noteType != 1){
theTag = theTag.parentNode;
}
if(theTag){
theTagName = theTag.tagName;
if(theTagName == 'TD'){
theTag.style.backgroundColor = '#3280ff';
}
}

Also, set the - backgroundColor - property of the element's - style -
object instead of using the deprecated - bgColor - property (unless you
are trying to actively support dinosaurs like Netscape 4).

<body onmouseover="chgColor()" onmouseout="chgBack()"

Pass the event object(s) to your function from the event handing
functions:-

<body onmouseover="chgColor(event)" onmouseout="chgBack(event)"

Richard Cornford skrev:
<snip>

Please do not top-post to comp.lang.javascript.

Richard.
 
K

Krij

Hi!

Thank you very much for answering and explaining a novice on javascript
:)
Not sure what you mean by "do not top-post to..."

Richard Cornford skrev:
 
K

Krij

Hi!

I tried this solution, but it will not work in either FF or IE. Anybody
that can see why?

function chgColor(ev){
var theTagName , theTag = ev.srcElement || ev.target;
while(theTag && theTag.nodeType != 1){
theTag = theTag.parentNode;
}

if(theTag){
theTagName = theTag.tagName;
if(theTagName == 'TD'){
theTag.style.backgroundColor = '#3280ff';
}
}

Likewise I have the same function code for chgChangeColorBack :)

and in the body tag an onMouseOver and onMouseOut call.


Krij skrev:
 
R

Richard Cornford

Krij said:
I tried this solution, but it will not work in either FF or IE. Anybody
that can see why?

function chgColor(ev){
var theTagName , theTag = ev.srcElement || ev.target;
while(theTag && theTag.nodeType != 1){
theTag = theTag.parentNode;
}

if(theTag){
theTagName = theTag.tagName;
if(theTagName == 'TD'){
theTag.style.backgroundColor = '#3280ff';
}
}

}

- missing closing brace (see the reported javascript (syntax) errors in
respective browsers).

<snip>

Putting your responses above what you quote of previous messages is
top-posting. Please do not do that in comp.lang.javascirpt. see:-

<URL: http://jibbering.com/faq/ >

Richard.
 
M

Martin Honnen

Krij wrote:

and in the body tag an onMouseOver and onMouseOut call.

Have you made the suggested change to pass in the event object to your
functions
<body onmouseover="chgColor(event)" onmouseout="chgBack(event)"

Check the JavaScript console of Firefox whether any script error is
indicated.
 
K

Krij

Richard Cornford skrev:
}

- missing closing brace (see the reported javascript (syntax) errors in
respective browsers).


<snip>

Putting your responses above what you quote of previous messages is
top-posting. Please do not do that in comp.lang.javascirpt. see:-

<URL: http://jibbering.com/faq/ >

Richard.

Thank you for clearing up that!

However, I do not get any error in either browsers :) There are just
two closing brackets, not three. That should just be a typo.

By the way, the book we're using is JS 1.5. Which version are we
'officially' at?
 
K

Krij

Krij skrev:
Richard Cornford skrev:


Thank you for clearing up that!

However, I do not get any error in either browsers :) There are just
two closing brackets, not three. That should just be a typo.

By the way, the book we're using is JS 1.5. Which version are we
'officially' at?

Hi again!

Sorry, I was wrong :-(

I do get an error in IE. And it's strange. It says: Line 40 Object
expected.

Now this means that in the <BODY> tag where I call the function there's
a missing object? However, I have made sure that the correct
onmouseOver contains the correct name of the function, like: <BODY
onmouseover="chgColor(event)" and likewise for the onmouseOut event

so....

it probably mean the error is somewhere else????

Anyway, thank you to all for taking time to solve this :)
 
K

Krij

Krij skrev:
Krij skrev:


Hi again!

Sorry, I was wrong :-(

I do get an error in IE. And it's strange. It says: Line 40 Object
expected.

Now this means that in the <BODY> tag where I call the function there's
a missing object? However, I have made sure that the correct
onmouseOver contains the correct name of the function, like: <BODY
onmouseover="chgColor(event)" and likewise for the onmouseOut event

so....

it probably mean the error is somewhere else????

Anyway, thank you to all for taking time to solve this :)

Hi again!

Just want to be clear on one thing:

In the original posting of code there was an error I think.

It read, in the function:

function chgColor(ev){
var theTagName , theTag = ev.srcElement || ev.target;
while(theTag && theTag.noteType != 1){
theTag = theTag.parentNode;

I changed 'noteType' to 'nodeType'. Was I wrong?
 
M

Martin Honnen

Krij said:
By the way, the book we're using is JS 1.5. Which version are we
'officially' at?

For web scripting it should not really matter to you. The scripting
engines in browsers like IE 5.5. and later, Netscape 6 and later,
Firefox 1.0 and later, Opera 7 and later very much support the
ECMAScript edition 3 which defines core language stuff like types,
expressions, statements, objects, functions. Then what matters for web
scripting is the DOM support.

As for the official JavaScript version we are at, Firefox 1.5 supports
JavaScript 1.6, Firefox 2.0 when released later this year is supposed to
support JavaScript 1.7.
But as said, it should not really matter to you unless you specifically
script for Firefox/Mozilla (e.g. XUL, extensions) because most changes
after JavaScript 1.5 are not supported in other browsers' scripting
engines and need to be specifically requested even in Firefox/Mozilla
(e.g. with
<script type="text/javascript; version=1.6">
).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top