onload focus tag works fine in IE, not in Netscape
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?
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
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.
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.
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.)
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:
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.
Code:
<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();
}
__________________
........ another wild guess ........
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.
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.
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....
__________________
........ another wild guess ........