Can't Get Basic Script to Work

P

PulsarSL

Hey,

I used to know javascript, but it's been years since I've used it. I'm
trying to relearn it, but I'm stuck already. Can anyone tell me why
this does nothing when I load the page?

Thanks
PulsarSL
---------

<html>
<head>

<script language="javascript">
function printTest()
{
alert("testing")
document.write("testing")
return()
}
</script>

</head>
<body onload="printTest()">

</body>
</html>
 
M

Michael Winter

sorry but can i ask why
<scrip language="javascript"> is deprecated?

The value of the language attribute is an unstructured string, defined
by vendors. I would surmise that this was deemed undesirable, and that a
well-known format - MIME types, in this case - would be preferable.

There are also reasons to avoid specific values in the language
attribute, as described in the FAQ notes[1].


You're rather late to ask this question; the attribute was deprecated
around eight years ago.

[snipped top-post]

Please do not top-post and full-quote to this group.

How do I quote correctly in Usenet?
<http://www.netmeister.org/news/learn2quote2.html>

You might want to investigate OE-QuoteFix
(<http://home.in.tum.de/~jain/software/oe-quotefix/>).

Mike


[1] <http://www.jibbering.com/faq/faq_notes/script_tags.html#hsAtln>
 
P

PulsarSL

Janwillem said:
"return" is not a function, use it without the parenthesis.

Thanks, this fixed it right up!

Wow -- I didn't realize it was deprecated so long ago. Very
interesting.

Thx,
PulsarSL
 
G

Guest

While it is not a function (nor is "if") one can use parenthesis and
the white space between the word and the parenthesis does not matter.

if (x==4)

or

if(x==4)

seem to work with no problems as does:

function square(x) {
return x*x OR
return (x*x) OR
return(x*x) OR
}

But return is not a function (as was pointed out). The parentheses are not
part of "return" (as they are in a function's syntax, myfunction()) but
simply delimiters for the expression returned. If you use them it forces
Javascript to evaluate that expression. The expression () (what does that
evaluate to?) results in an error and the default (unless you use try/catch
to trap errors) is to stop at an error.
 
R

Richard Cornford

Spamless said:
While it is not a function (nor is "if") one can use parenthesis
and the white space between the word and the parenthesis does
not matter.

if (x==4)

or

if(x==4)

Generally javascript will allow any amount of white space to appear
between language tokens of any sort. There are some restrictions on the
contexts in which white space that is also a line terminator may appear,
But you could write your - if - statement as:-

if
(
x
==
4
)
{
...
}

- without causing any issues to the language interpreter (humans on the
other hand would tend to be unhappy to be asked to read it).
seem to work with no problems as does:

function square(x) {
return x*x OR
return (x*x) OR
return(x*x) OR
}

But return is not a function (as was pointed out).
The parentheses are not part of "return" (as they are in
a function's syntax, myfunction())

There is not really any "function's syntax, myfunction()". That is a
CallExpression, which is made up of two parts; a MemberExpression (or
another CallExpression) followed by an Arguments expression, where
Arguments are parentheses surrounding a possibly empty ArgumentsList.
This parenthesised Arguments expression can be regarded as an operator
that acts upon the MemberExpression as its operand; resolving the
MemberExpression as a function and calling it (passing any contained
arguments on to the call).
but simply delimiters for the expression returned.

Those parenthesise are not delimiters. They are actually an Expression
in their own right; a PrimaryExpression consisting of the parenthesise
sounding another Expression. This primary expression is evaluated at run
time, evaluating to the value of the result of the evaluation of the
Expression contained in the parenthesise. (Interpreter optimisations
should allow many parenthesised expressions to be silently removed and
replaced with the contained Expression, so they will not actually be
evaluated at run-time.)
If you use them it forces Javascript to evaluate that
expression.

The expression would be evaluated anyway. The parenthesise allow the
precise order of evaluation of a larger expression to be controlled, and
can be used to express the expected order of evaluation in the source
code (for clarity), or even to allow white-space free source code to be
unambiguously tokenised.
The expression ()

The only javascript production that allows that as an expression is
Arguments. It can only be interpreted as an attempt to call a
left-hand-side that is a function. The Expression in -
PrimaryExpression: ( Expression ) - is not optional.
(what does that evaluate to?) results in an error and
the default (unless you use try/catch to trap errors)
is to stop at an error.

Try-catch will not help at all in this context as Arguments following
the - return - keyword is a syntax error (return is not a
MemberExpression or a CallExpression). Syntax errors are triggered prior
to code execution and try-catch can only catch runtime errors (only the
use of - eval - or the Function constructor could produce a syntax error
at runtime and so be caught with try-catch).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top