javascript issue

They have: 461 posts

Joined: Jul 2003

am i using the wrong javascript funtions or is it something else causing this not to work... whille the code is prepped for a php file, it's actually from an html file i whipped up to get the javascript right so i know any future issues are either with php or the db

(note: carriage returns in tags added to stop horizontal scroll)

<html><head></head><body><center>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" name="new_friend"
method="POST" onReset="window.close();">
<table frame="void" bgcolor="#000000" border="0" cellpadding="0"
cellspacing="0" text="#c8c8c8">
<tr>
<td>Selected Friend</td>
<td><input type="text" name="fun" readonly size="15"
value="<?php echo $_GET['un']; ?>"></td>
</tr>
<tr>
<td colspan="2">
<textarea cols="30" name="cof" onKeyUp="return findlength();" rows="4" wrap="virtual">
Enter a Comment or clear this. You have a maximum of 255 characters, everything above
that will be lost
</textarea>
</td>
</tr>
<tr><td colspan="2">Characters in Comment:<input type="text" name="chars"
readonly size="3" value="0"></td>></tr>
<tr>
<td><input type="submit" value="Add My Friend!"></td>
<td><input type="reset" value="Not My Friend"></td>
</tr>
</table>
</form>
&lt;script language="javascript"&gt;
function findlength(){
var comment=document.new_friend.cof.value(); // get the comment
var numchars=comment.length; // find the number of characters
document.new_friend.chars.write(numchars);
// let the user know the number of characters
}
&lt;/script&gt;
</center>
</body>
</html>
'and the variation i made thinking maybe i could moorph it a bit to look like something else i made that does work....
<html><head></head><body><center>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" name="new_friend"
method="POST" onReset="window.close();">
<table frame="void" bgcolor="#000000" border="0" cellpadding="0"
cellspacing="0" text="#c8c8c8">
<tr>
<td>Selected Friend</td>
<td><input type="text" name="fun" readonly size="15"
value="<?php echo $_GET['un']; ?>"></td>
</tr>
<tr>
<td colspan="2">
<textarea cols="30" name="cof" onKeyUp="return findlength();" rows="4" wrap="virtual">
Enter a Comment or clear this. You have a maximum of 255 characters, everything above
that will be lost
</textarea>
</td>
</tr>
<tr><td colspan="2"><iframe name="chars" src="chars.html"></iframe></td></tr>
<tr>
<td><input type="submit" value="Add My Friend!"></td>
<td><input type="reset" value="Not My Friend"></td>
</tr>
</table>
</form>
&lt;script language="javascript"&gt;
function findlength(){
var comment=document.new_friend.cof.value(); // get the comment
var numchars=comment.length; // find the number of characters used
parent.chars.document.open();
parent.chars.document.write("<html><body>Your comment is "+numchars+
" long.</body></html>");  // tell the user the number of character used
parent.chars.document.close();
}
&lt;/script&gt;
</center>
</body>
</html>
'

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

<?php
<html>
<
head>
<
em>
&
lt;script language=\"javascript\" <strong>type=\"text/javascript\"</strong>&gt;
function findlength(){
var comment=document.new_friend.cof.value(); // get the comment
var numchars=comment.length; // find the number of characters used
parent.chars.document.open();
parent.chars.document.write(\"<html><body>Your comment is \"+numchars+
\" long.</body></html>\");  // tell the user the number of character used
parent.chars.document.close();
}
&lt;/script&gt;
</em>
<<strong>/</strong>head><body><center>
<form action=\" echo
$_SERVER[PHP_SELF]; \" name=\"new_friend\"
method=\"POST\" onReset=\"window.close();\">
<table frame=\"void\" bgcolor=\"#000000\" border=\"0\" cellpadding=\"0\"
cellspacing=\"0\" text=\"#c8c8c8\">
<tr>
<td>Selected Friend</td>
<td><input type=\"text\" name=\"fun\" readonly size=\"15\"
value=\" echo
$_GET['un']; \"></td>
</tr>
<tr>
<td colspan=\"2\">
<textarea cols=\"30\" name<strong>-</strong>\"cof\" onKeyUp=\"return findlength();\" rows=\"4\" wrap=\"virtual\">
Enter a Comment or clear this. You have a maximum of 255 characters, everything above
that will be lost
</textarea>
</td>
</tr>
<tr><td colspan=\"2\"><iframe name=\"chars\" src=\"chars.html\"></iframe></td></tr>
<tr>
<td><input type=\"submit\" value=\"Add My Friend!\"></td>
<td><input type=\"reset\" value=\"Not My Friend\"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
?>

The function needs to be before you call it. You can't call a function that doesn't exist yet.

The parts in bold are errors. (Including two opening head tags, but no closing tag, and a minus sign instead of an equal sign).

They have: 461 posts

Joined: Jul 2003

i have a set of pages for finding hexvalues for colors as well as displaying mixes, and the javascript is the last thing int he file... i don't have the type call in it, yet that works fine in every browser i've used...

anyone know why?

i've usedjavascript before but always had it at the end and without type (and called by a form) and it's always worked perfectly

edit:
ahhhh! tried it WITH your adjustments and it's still not updating for me

i'd prefer the textbox since it was both my first idea and i think it looks better. any chance anyone can help??? obviously those error were good catches, but....

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

You're right, by calling it from the form, the JavaScript has already loaded and is available. I totally blanked on the fact that it was being activated by the form.

I tend to keep the JavaScript in a library, rather than in script tags, so it goes best in the head element when you do it that way.

Also, type="text/javascript" is the proper way to identify it. You can omit the type and language altogether and it will work in most browsers, but it's not valid, and it's not good coding practice. Newer browsers and XHTML require the type, not the language, attribute.

As for the errors, my brain's working in PHP at the moment, but the only thing that stands out is that I'm not sure how the iframe would update, having never done it before that way. I would likely choose to use an alert.

If you use HTML's maxsize attribute for the textarea, you can limit the textarea to 255 characters and they will be UNABLE to type more. Smiling

They have: 461 posts

Joined: Jul 2003

the thml book i have is orielly's webmaster in a nutshell 2nd edition. i think it says the language is required.
it's nice to know the change for xhtml. i'm trying to pick those up as i do this and work some of it in. thnx for the fyi there.

i KNOW it doesn't have maxsize listed as a possible textarea attibute, so i didn't realize that was possible. i thought they listed ALL attributes and event handlers.

i know it works for regular frames.
this is to show you the place i did something that i tried ot morph on: http://people.brandeis.edu/~m3rajk/JMT/color/

i thought i was suppossed to lose that space on aug 1, so i don't knwo how long it'll remain up

edit: found the errors...

function (for the first one) needed to be changed to...

function findlength(){
var comment=document.new_friend.cof.value;
var numchars=comment.length;
document.new_friend.chars.value=numchars;
}
'

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.