Jump to content

if variable is undefined?


mortalc

Recommended Posts

I have put a code into Dreamweaver, and it says that there is a syntax error on a line that reads:

if !(test == undefined)

As you can probably guess (or not), I'm trying to write "if test has not been set a value".What have I done wrong?

Link to comment
Share on other sites

Conditions need parenthesis around their argument:

if (!(test == undefined))

Link to comment
Share on other sites

Note that for comparing to values such as undefined or null it's probably best to use strict comparison:

if (!(test === undefined)) {

This would also do the same thing with less parens:

if (test !== undefined) {

Link to comment
Share on other sites

Note that text must have been declared someplace before this comparison can work, either in a var statement or as function argument or by direct assignment. One of these will do it:test = "";var test;function foo(test) {Otherwise, you'll throw an undefined variable error. (I know that seems dumb, since that's what you're testing for, but that's the way it is.)I second the use of the !== operator for this task

Link to comment
Share on other sites

It would have been set with a prompt box.Off-topic, same program: is it possible to include a form (hidden) in javascript, specifically an if statement? Also, how can I set the "value" of a hidden input to a variable?e.g.

var x = ...;if (...) {<form><input type="hidden" name="name" value="x" /></form>//PHP form processing}

Link to comment
Share on other sites

You can use CSS to hide a form, and manipulate that value with JavaScript. The value attribute of any HTMLInputElement holds the value of the element.

Link to comment
Share on other sites

If the script is running as the page loads you can use document.write to accomplish that task. For example:

<body><script type='text/javascript'>if (condition == true) {document.write("<form action=''>");document.write("<input type='hidden' value='"+aVariable+"' />"); //Set value with variabledocument.write("</form>");}</script>....</body>

If the script is inside a function it won't work. It has to be directly inside the script tags. Also notice the placement of the script tags, they should be placed where ever it is you want the form to appear.I also answered your second question about setting the value with a variable.

Link to comment
Share on other sites

Ok. so would that set the "value" as variable a?Can variables carry across from one script tag from another? so:

<script type="text/javascript">var a = 3;</script>if (a === 3) {...}

Link to comment
Share on other sites

Ok. so would that set the "value" as variable a?
It will set it as the value of aVariable.
Can variables carry across from one script tag from another?
Yes (but you need a second script block in your example).
Link to comment
Share on other sites

Ok. so would that set the "value" as variable a?Can variables carry across from one script tag from another? so:
Yes.To your first question: It would set the value of the hidden input to whatever value the variable contains.To the second: If they are declared globally, yes. So this will work:
<head>   <script type='text/javascript'>   var a = 5;   </script></head><body><script type='text/javascript'>var x = 7;if (a != x) {   //Do something}</script>

EDIT: I gotta type faster! Synook beats me to the punch! :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...