Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-28-2012, 04:44 PM   PM User | #1
SteveH
Regular Coder

 
Join Date: Nov 2005
Posts: 615
Thanks: 91
Thanked 1 Time in 1 Post
SteveH is an unknown quantity at this point
Correct syntax query

Hello

Can I ask, please, what is the best way of writing the script here (Web form):

Code:
<table border="0" cellspacing="1">
		<tr>
			<td valign="top">
				<p class="mySize">Name:</p>
			</td>
			<td colspan="2">
				<input name="NAME" "class=normal" name="ContactUs_Name" size="35" value="<% =ContactUs_Name %>">onblur="toggleColor(this)" onfocus="toggleColor(this)">

			</td>
In my <head> tag I also have
Code:
<STYLE>
.normal { background-color: #C0D4D2; color: #000000; }
.focus  { background-color: #9DF7EE; color: #000000; }
</STYLE>

<SCRIPT LANGUAGE="JavaScript">
function toggleColor(objElement)
{
  if (objElement.className=='normal')
    objElement.className='focus';
  else
    objElement.className='normal';
}
</SCRIPT>
followed by:
Code:
<BODY onLoad="document.all.NAME.focus()">
This produces the result on the screenshot I have attached.

Thanks for any advice.
Attached Thumbnails
Click image for larger version

Name:	CodeForum.jpg
Views:	65
Size:	18.0 KB
ID:	11101  
SteveH is offline   Reply With Quote
Old 04-28-2012, 10:18 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
One of your quotes is out of place, and you gave this input two names. It can have only one (kind of like The Highlander). And you have an extra > that shouldn't be there.
Code:
<input name="NAME" class="normal" name="ContactUs_Name" size="35" value="<% =ContactUs_Name %>">onblur="toggleColor(this)" onfocus="toggleColor(this)">
Also, instead of using document.all, use document.forms["{formname}"].
Code:
<BODY onLoad="document.forms["formname"].NAME.focus()">
^_^

Last edited by WolfShade; 04-28-2012 at 10:22 PM..
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
SteveH (04-28-2012)
Old 04-28-2012, 10:36 PM   PM User | #3
SteveH
Regular Coder

 
Join Date: Nov 2005
Posts: 615
Thanks: 91
Thanked 1 Time in 1 Post
SteveH is an unknown quantity at this point
Many thanks for that Wolfshade - it didn't like

Code:
<BODY onLoad="document.forms["formname"].NAME.focus()">
but the ugly script on the Web page has now gone.

Cheers
SteveH is offline   Reply With Quote
Old 04-29-2012, 06:21 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If you continue to use document.all your code will only work in MSIE.

If that doesn't bother you, and you don't care if your JS is obsolescent, then leave it alone.

Otherwise, find a way to avoid using document.all.

If you don't have a <form> on the page, then you could do
Code:
<body onload="document.getElementsByName("NAME")[0].focus();">
assuming that you only have one name="NAME" on the page or at least that the one you care about is the first one.

But now...What does any of this have to do with ASP? It's in the wrong forum. It should be in the JavaScript forum.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
SteveH (04-29-2012)
Old 04-29-2012, 01:57 PM   PM User | #5
SteveH
Regular Coder

 
Join Date: Nov 2005
Posts: 615
Thanks: 91
Thanked 1 Time in 1 Post
SteveH is an unknown quantity at this point
Oh, I didn't know it was an IE thing. I have a form with only one field with NAME (the name field) and used your suggestion and it seems to work fine.

Thanks
SteveH is offline   Reply With Quote
Old 04-29-2012, 09:52 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
Quote:
Originally Posted by SteveH View Post
Many thanks for that Wolfshade - it didn't like

Code:
<BODY onLoad="document.forms["formname"].NAME.focus()">
but the ugly script on the Web page has now gone.

Cheers
That's because you used a quote inside a quote, truncating everything after the second " . You should either use ' for the onload, or escape the inner " with a backslash. (Sorry I didn't notice this, earlier)

Code:
<BODY onLoad='document.forms["formname"].NAME.focus()'>
 . . OR . . 
<BODY onLoad="document.forms[\"formname\"].NAME.focus()">
WolfShade is offline   Reply With Quote
Old 04-30-2012, 01:37 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No, the backslash trick will *NOT* work.

Remember, it is HTML that is parsing that, so that it can then invoke JavaScript.

HTML does *NOT* pay attention to \" as anything special.

To HTML you would have
Code:
<body onload="document.forms[\"     formname\      "].NAME.focus()">
And it would try to invoke JavaScript with just
Code:
    document.forms[\
The first answer is okay, but the preferred way in modern XHTML is
Code:
<body onload="document.forms['formname'].NAME.focus()">
because (a) all tag names and attributes should be lower case and (b) all attribute values should be given in true "..." quotes, not '...'.

Though even that is obsolescent as named forms are considered out of date. You should, instead, give the form an ID and then do
Code:
<body onload="document.getElementById('formid').NAME.focus()">
Again, this all belongs in the JavaScript forum, rather than here.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:25 PM.


Advertisement
Log in to turn off these ads.