Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Unable to load javascrip

Reply
Thread Tools

Unable to load javascrip

 
 
Terje Sæternes
Guest
Posts: n/a
 
      10-29-2004
It seems that this script wont run, any idea what I have don wrong+

Code-----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
<!--
function isblank(s) {
for (var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
}
return true;
}



function SetRegAction() {

// WebBrukerID
if (isblank(document.form1.test.value)) {
alert ("Fornavn må fylles ut.");
document.form1.test.focus();
return (false);
}
//-->
</script>
</head>

<body>
<form name="form1" method="post" action="" onsubmit="javascript:return
SetRegAction()">
<input name="test" type="text" id="test">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


 
Reply With Quote
 
 
 
 
Terje Sæternes
Guest
Posts: n/a
 
      10-29-2004
Problemo solved, it was the missing "}" aarghhh


Terje

"Terje Sæternes" <(E-Mail Removed)> skrev i melding
news:Tfqgd.60823$(E-Mail Removed) ...
> It seems that this script wont run, any idea what I have don wrong+
>
> Code-----
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <title>Untitled Document</title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> <script language="javascript" type="text/javascript">
> <!--
> function isblank(s) {
> for (var i = 0; i < s.length; i++) {
> var c = s.charAt(i);
> if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
> }
> return true;
> }
>
>
>
> function SetRegAction() {
>
> // WebBrukerID
> if (isblank(document.form1.test.value)) {
> alert ("Fornavn må fylles ut.");
> document.form1.test.focus();
> return (false);
> }
> //-->
> </script>
> </head>
>
> <body>
> <form name="form1" method="post" action="" onsubmit="javascript:return
> SetRegAction()">
> <input name="test" type="text" id="test">
> <input type="submit" name="Submit" value="Submit">
> </form>
> </body>
> </html>
>
>



 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      10-29-2004
On Fri, 29 Oct 2004 13:58:49 +0200, Terje Sæternes <(E-Mail Removed)>
wrote:

[snip]

> <script language="javascript" type="text/javascript">


The language attribute is deprecated, and the (required) type attribute
makes it redundant. Remove it.

> <!--


Hiding SCRIPT element content is an obsolete practice. All user agents now
in use understand what a SCRIPT element is, even if they cannot execute
it. Scripts should usually be placed in external files anyway, hiding the
script automatically.

> function isblank(s) {
> for (var i = 0; i < s.length; i++) {
> var c = s.charAt(i);
> if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
> }
> return true;
> }


This is a really inefficient way of performing this sort of test. Use a
regular expression:

function isBlank(v) {
return /^\s*$/.test(v);
}

The regular expression will match any string that is either empty, or
solely composed of whitespace characters[1].

> function SetRegAction() {


Submission handlers should generally be written to take one argument: a
reference to the form. This means you don't have to hard-code the form
name.

function SetRegAction(form) {
// ...

You'd call this from the FORM with:

<form ... onsubmit="SetRegAction(this);">

Note that you don't need "javascript:". It only makes a difference in one
browser (IE), and even then, its only necessary if you've been using
scripting languages other than Javascript. Most browsers will see that
string a label, and ignore it.

Based on this change, you could modify the following to:

// Save a reference to the 'test' control
var test = form.elements['test'];

> if (isblank(document.form1.test.value)) {


if(isBlank(test.value)) {

> alert ("Fornavn må fylles ut.");
> document.form1.test.focus();


test.focus();

> return (false);


return false;

The parentheses aren't necessary.

> }


[snip]

> <form name="form1" method="post" action=""


Now you don't need to name the form (you should use better names, anyway).

> onsubmit="javascript:return SetRegAction()">
> <input name="test" type="text" id="test">


You do realise that this control wouldn't be submitted, don't you?

> <input type="submit" name="Submit" value="Submit">


Unless you have multiple submit buttons which represent different actions,
you generally don't need to name them. In such situations, something like:

<input type="submit" name="operation" value="Delete">

would be more appropriate. Certainly be careful not to use names that are
properties of the FORM object. Naming a control, action, for example, will
prevent you from accessing the action property on the FORM.

[snip]

Hope that helps,
Mike


[1] A more thorough explanation:

Regular expressions are composed of a series of productions that form a
pattern. Certain characters have special meanings, causing the pattern to
evaluated in a certain way. In the expression I used, there are four
tokens, each of which is special.

The first, ^, means that the pattern that follows must match the beginning
of the input. So, for example, ^some would match any string that began
with "some"; "something", "sometime", "some chicken", etc.

The last, $, means that the pattern that precedes the token must match the
end of the input.

The escape sequence, \s, matches whitespace characters.

The token, *, is a quantity specifier that means the preceding token can
exist zero or more times.

All together, you get a pattern that will match an empty string, or one
which contains only whitespace.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      10-29-2004
"Terje Sæternes" wrote:

> It seems that this script wont run, any idea what I have don wrong+
>
> Code-----
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <title>Untitled Document</title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> <script language="javascript" type="text/javascript">


<script type="text/javascript">

The -language- attribute is deprecated and not required.

> <!--


Not needed. Remove.

> function isblank(s) {
> for (var i = 0; i < s.length; i++) {
> var c = s.charAt(i);
> if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
> }
> return true;
> }
>
> function SetRegAction() {
>
> // WebBrukerID
> if (isblank(document.form1.test.value)) {
> alert ("Fornavn må fylles ut.");
> document.form1.test.focus();
> return (false);
> }


You're missing a closing -}- here.

> //-->


Not needed. Remove.

> </script>
> </head>
>
> <body>
> <form name="form1" method="post" action="" onsubmit="javascript:return
> SetRegAction()">


onsubmit="return SetRegAction();"

> <input name="test" type="text" id="test">
> <input type="submit" name="Submit" value="Submit">


This is fine, you've named your submit button "Submit". Be careful though,
if you named your submit button "submit" (note the lower case 's') you could
run into problems later if you attempt to submit the form programmatically
(ie - document.forms['theForm'].submit()).

> </form>
> </body>
> </html>


--
Grant Wagner <(E-Mail Removed)>
comp.lang.javascript FAQ - http://jibbering.com/faq

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascrip call interferes with OnClick settings MindofZiggi Software 1 07-25-2008 11:33 AM
call javascrip from master page's content Ganesh ASP .Net 1 08-02-2007 06:54 PM
How do i call JAVASCRIP function if... =?utf-8?B?4pmlZVh0cmVtZSBvWHlnZW7imaU=?= ASP .Net 1 08-25-2006 07:57 AM
Disable Calendar Control from javascrip Robert Ladd ASP .Net 0 02-27-2005 07:16 AM
see two problem(asp web server control(dropdownlist) vs. javascrip deepak kumar ASP .Net Web Controls 0 10-30-2004 03:02 AM



Advertisments