...

View Full Version : onload focus tag works fine in IE, not in Netscape



dbast
09-25-2003, 04:17 PM
I have a simple "email to a friend" form. When users get there, I want the cursor positioned in the first field of the form -- the FromName field.

The code works perfectly fine in IE 6. It will not work in Netscape Communicator 4.76. In Netscape, the cursor does not get positioned correctly and "JavaScript error" comes up at the bottom of the screen.

Seems to me this should be simple -- what am I doing wrong here?

<BODY onload="emailform.FromName.focus()">

Vincent Puglia
09-25-2003, 04:27 PM
Hi,

1) do you have form tags around the field? IE don't care, virtually everyone else does.
2)<BODY onload="document.emailform.FromName.focus()"> -- a full DOM path is required

Vinny

dbast
09-25-2003, 04:39 PM
Vinny: Thanks for the quick reply.

Not sure what you mean by form tags around the field. This is a ColdFusion form, so I have <CFFORM> and </CFFORM> around the form itself, and <CFINPUT> tags around each field. Is it maybe the ColdFusion aspect that's giving me trouble?

I didn't have the full DOM path, but now do as you suggested. Still OK in IE, still not OK in Netscape.

Vincent Puglia
09-25-2003, 04:59 PM
Hi Diane,


Is it maybe the ColdFusion aspect that's giving me trouble?

Quite possibly, but I don't know for sure. I presume the form is visible in NN4. Type 'javascript:' in the address bar and see what error message you are getting.

Vinny

Roy Sinclair
09-25-2003, 05:00 PM
You used an IE proprietary shortcut which explains why it works in IE but not Netscape.

Try:

<BODY onload="document.forms.emailform.FromName.focus()">

cheesebag
09-25-2003, 05:01 PM
Type "mocha" in the address bar, hit "enter", and let us know what the error message is. :)

dbast
09-25-2003, 05:11 PM
Thank you all -- maybe now we're getting somewhere?

I typed javascript: into the address bar, as Vinny suggested, and got this:

JavaScript Error:
http://www.heartland.org/EmailFriend.cfm?artId=12909, line
185:

document.forms.emailform has no properties.


What kind of properties might it be looking for?

------

I also tried changing my onload tag to look as Roy suggested,
<BODY onload="document.forms.emailform.FromName.focus()">,
but that didn't help. (didn't hurt either, which is always a good thing.)

cheesebag
09-25-2003, 05:15 PM
Do you have anything in the document set with a CSS 'position:absolute' (or 'relative')?

Vincent Puglia
09-25-2003, 05:22 PM
Hi Diane,

Is it common practice with coldFusion to put a document inside a document?



<html>
......
<body>
<BODY onload="document.forms.emailform.FromName.focus()">
<html>
<head>
....


Vinny

Vincent Puglia
09-25-2003, 05:26 PM
I didn't look at all the code, but if you are writing the second 'body' somewhere up top, NN4 may be trying to execute the onload before the form is written. In which case, the form has no properties.

Try putting the ''document....focus()" just before the closing body tag:

<script language='javascript' type='text/javascript'>
document...focus()
</script>
</body>

Vinny

cheesebag
09-25-2003, 05:36 PM
OK...your HTML is, well, a mess. Two body tags, two </head> tags, scripts stuck in between head #2 & body #2...eeesh. All that needs to be fixed. And all that 'dynamic' HTML output is completely off the map, no way to build a page.

Here's the focus thing: Navigator 4 generates a Layer object every time you use CSS to set an element to position:absolute/relative. Don't ask why, it's a miserable decision made back in 1997. Since you've set your <div ID="contentmiddlenav"> to position:absolute, the form inside it is now in a stinkin' layer, which means it's essentially in another window. Great.


<script language="javascript" type="text/javascript">
var Explorer4 = (document.all);
var Netscape6 = (document.getElementById);
var Netscape4 = (document.layers);

onload = function()
{
if (Netscape4)
document.contentmiddlenav.document.emailform.FromName.focus();
else document.emailform.FromName.focus();
}

Vincent Puglia
09-25-2003, 05:41 PM
Gee cheesebag,

Glad you had the patience to read all the code. Guess I should have drunk some 'mocha' :D

Vinny

dbast
09-25-2003, 05:44 PM
Vinny, your latest post was right on target!

You were right not to look at all the code -- I'm including templates and other Cold Fusion stuff that makes this a messy page to look at in source view.

But I put this code

<BODY onload="document.forms.emailform.FromName.focus()">

immediately after the form and the close of a series of If statements that help define who gets what kind of email. The onload tag is followed by a couple of Cold Fusion templates, then the </body> tag. It works swimmingly now.

Wanted to thank you all for this exchange. I'm new to CodingForums but will be back! Great help, and fast! Thanks again.

cheesebag
09-25-2003, 05:56 PM
Diane...

Sorry to disappoint you, but putting that body tag inside the layer (in between the <div></div> tags) was what 'fixed' it. The onload handler then runs in the scope of the layer, not the main window. This is not the proper way to do it, but I'll leave it at that....

dbast
09-25-2003, 06:00 PM
Cheesebag:

I'll work on the mess. Am building on CF templates others have written, and I know it's not a pretty site/sight!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum