If Then statement

S

shank

I'm coming from the ASP worl and have no clue to javascript. In the
following code, I'm trying to set the value for variable strPage, then use
that in the redirect statement. The page does nothing. No redirect at all.
What should the synatx be?
thanks!

<SCRIPT LANGUAGE="JavaScript">
if Session("SoftHard") = "Hard"
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");
}
if Session("SoftHard") = "Soft"
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");
}
setTimeout("top.location.href = strPage",4000);
</SCRIPT>
 
A

artemis.alpeia

Il Mon, 21 Jul 2003 21:35:08 GMT, "shank" <[email protected]> ha
scritto nel messaggio
I'm coming from the ASP worl and have no clue to javascript. In the
following code, I'm trying to set the value for variable strPage, then use
that in the redirect statement. The page does nothing. No redirect at all.
What should the synatx be?
thanks!

<SCRIPT LANGUAGE="JavaScript">
if Session("SoftHard") = "Hard"
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");
}
if Session("SoftHard") = "Soft"
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");
}
setTimeout("top.location.href = strPage",4000);
</SCRIPT>

I guess Session is a function of yours :/

the if statement requires parenthesis to include the boolean
condition.

furthermore, to verify an equality you must use a double equal (==).

if (Session("SoftHard") == "Hard")
{
// blah blah
}
else if (Session("SoftHard") == "Soft")
{
// blah blah
}
 
S

shank

Thanks, but still not redirecting. Using your example I have the following.
I'm assuming in lieu of // blah blah, my statements were correct. I'm
guessing that my problem is implementing my Session variables
(Session("OrderNo")"into the mix. Any thoughts?
thanks!

<SCRIPT LANGUAGE="JavaScript">
if (Session("SoftHard") == "Hard")
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");
}
else if (Session("SoftHard") == "Soft")
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");
}
setTimeout("top.location.href = strPage",1000);
</SCRIPT>
 
L

Lasse Reichstein Nielsen

shank said:
I'm coming from the ASP worl and have no clue to javascript.

Javascript syntax is derived from C syntax, just like C++, Java, C#,
and a lot of other languages. The short version is:

- case sensitive!
- conditional and loop statements are:
if ( <expr> ) <stmt> [ else <stmt> ]
while ( <expr> ) <stmt>
do <stmt> while ( <expr> )
for (<expr> ; <expr> ; <expr>) <stmt>
where parentheses are needed (but [ ] just means "optional")
- statements are ended with semicolon, or grouped with "{" and "}"
- comparison is "==", while "=" is assignment.
- local variables are declared with "var" in front.

I don't know of any tutorials on Javascript that I can recommend, but
I am sure you can find a lot by entering "javascript tutorial" into
Google. Just be careful, they are not all good.
In the following code, I'm trying to set the value for variable
strPage, then use that in the redirect statement. The page does
nothing. No redirect at all. What should the synatx be? thanks!

If you used a browser capable of giving useful error messages (that
is, anythin but IE), then you would probably be told where the syntax
errors are. Even IE's error messages are better than nothing, and will
at least tell you what line the error is on.
<SCRIPT LANGUAGE="JavaScript">

should be
<script type="text/javascript">
The language attribute is deprecated and the type attribute is mandatory
in HTML 4.
if Session("SoftHard") = "Hard"

Should be
if (Session("SoftHard") == "Hard")
Comparison is "==", where "=" is assignment.
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");

should be
strPage = "hardpage.asp?OrderNo=" + Session("OrderNo");
The string concatenation operator is "+".
}
if Session("SoftHard") = "Soft"

could be changed into just
else
if Session("SoftHard") only has two results.
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");

String concatenation again.


/L
 
D

Douglas Crockford

I'm coming from the ASP worl and have no clue to javascript. In the
following code, I'm trying to set the value for variable strPage, then use
that in the redirect statement. The page does nothing. No redirect at all.
What should the synatx be?

There's nothing like knowing what you are doing. I recommend that you get a good
book and try to develop some competence in the subject. Flanagan's book is the
best available. Check the FAQ.

http://www.crockford.com/javascript/javascript.html
 
S

shank

Still not getting it right. Am I incorporating my Session("OrderNo")
variable correctly?
thanks!

<SCRIPT LANGUAGE="JavaScript">
if (Session("SoftHard") == "Hard")
{
var strPage = "hardpage.asp?OrderNo=" var Session("OrderNo");
}
else if (Session("SoftHard") == "Soft")
{
var strPage = "softpage.asp?OrderNo=" var Session("OrderNo");
}
setTimeout("top.location.href = var strPage",1000);
</SCRIPT>

Lasse Reichstein Nielsen said:
shank said:
I'm coming from the ASP worl and have no clue to javascript.

Javascript syntax is derived from C syntax, just like C++, Java, C#,
and a lot of other languages. The short version is:

- case sensitive!
- conditional and loop statements are:
if ( <expr> ) <stmt> [ else <stmt> ]
while ( <expr> ) <stmt>
do <stmt> while ( <expr> )
for (<expr> ; <expr> ; <expr>) <stmt>
where parentheses are needed (but [ ] just means "optional")
- statements are ended with semicolon, or grouped with "{" and "}"
- comparison is "==", while "=" is assignment.
- local variables are declared with "var" in front.

I don't know of any tutorials on Javascript that I can recommend, but
I am sure you can find a lot by entering "javascript tutorial" into
Google. Just be careful, they are not all good.
In the following code, I'm trying to set the value for variable
strPage, then use that in the redirect statement. The page does
nothing. No redirect at all. What should the synatx be? thanks!

If you used a browser capable of giving useful error messages (that
is, anythin but IE), then you would probably be told where the syntax
errors are. Even IE's error messages are better than nothing, and will
at least tell you what line the error is on.
<SCRIPT LANGUAGE="JavaScript">

should be
<script type="text/javascript">
The language attribute is deprecated and the type attribute is mandatory
in HTML 4.
if Session("SoftHard") = "Hard"

Should be
if (Session("SoftHard") == "Hard")
Comparison is "==", where "=" is assignment.
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");

should be
strPage = "hardpage.asp?OrderNo=" + Session("OrderNo");
The string concatenation operator is "+".
}
if Session("SoftHard") = "Soft"

could be changed into just
else
if Session("SoftHard") only has two results.
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");

String concatenation again.


/L
 
R

Richard Cornford

shank said:
Still not getting it right. Am I incorporating my
Session("OrderNo") variable correctly?
var strPage = "softpage.asp?OrderNo=" var Session("OrderNo");
<snip>

Probably not! You have not shown us your - Session - function but if it
returns a string (or a number) then you would want to concatenate it to
the URL string. The JavaScript concatenation operator is + (the plus
symbol, and it also performs numeric addition, but if either operand is
a string it performs concatenation (with automatic type conversion to
string if one operand is not a string)):-

var strPage = "softpage.asp?OrderNo=" + Session("OrderNo");

The - var - keyword is a syntax error in the context in which you used
it just before the Session function call. var - declares a variable and
should be followed by a valid identifier (and possibly an assignment
operation) or a comma separated list of identifiers with optional
assignments:-

var a;
var b = 'anyString';
var c, d, e = 24, f, g = new Object();

However, I suspect that what appears to be a function called - Session -
is actually the cause of your problems and represents a misconception
caused by your server-side experience with built in Session objects in
ASP. I may be wrong, in any event it would be useful if you would show
the function definition (if one exists) or explain what role Session is
expected to play in this code.

Richard.
 
T

Tim Slattery

shank said:
I'm coming from the ASP worl and have no clue to javascript. In the
following code, I'm trying to set the value for variable strPage, then use
that in the redirect statement. The page does nothing. No redirect at all.
What should the synatx be?
thanks!

<SCRIPT LANGUAGE="JavaScript">
if Session("SoftHard") = "Hard"

Use a double equal sign for comparison: if Session("SoftHard") ==
"Hard". Single equal sign is only for assignment.
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");

If you're trying to concatenate these things, use "+".
}
if Session("SoftHard") = "Soft"

Again, double equal sign for comparison
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");

And a + sign for concatenation
 
S

Steven Dilley

shank said:
I'm coming from the ASP worl and have no clue to javascript. In the
following code, I'm trying to set the value for variable strPage, then use
that in the redirect statement. The page does nothing. No redirect at all.
What should the synatx be?
thanks!

<SCRIPT LANGUAGE="JavaScript">
if Session("SoftHard") = "Hard"
{
strPage = "hardpage.asp?OrderNo=" & Session("OrderNo");
}
if Session("SoftHard") = "Soft"
{
strPage = "softpage.asp?OrderNo=" & Session("OrderNo");
}
setTimeout("top.location.href = strPage",4000);
</SCRIPT>
Is this a server script or a client script? Sessions are a server concept,
but redirection only works on the client side.

Have you considered learning vbscript first?
 
S

shank

I am in fact trying to incorporate a server side variable. There must be
some way to bring that variable locally so it can be used... isn't there?
thanks!
 
S

shank

I'm trying to assemble all the suggestions and I'm still not getting a
redirect to happen.
To simplify matters, I'm using 2 variables.
Any help is appreciated!!!
thanks!

<SCRIPT LANGUAGE="JavaScript">
var SoftHard == <%=Session("SoftHard")%>
var OrderNo == <%=Session("OrderNo")%>
if SoftHard = "Hard")
{
strPage = "hardpage.asp?OrderNo=" + OrderNo;
}
else if SoftHard = "Soft")
{
strPage = "softpage.asp?OrderNo=" + OrderNo;
}
setTimeout("top.location.href = " + strPage,1000);
</SCRIPT>
 
R

Richard Cornford

shank said:
I'm trying to assemble all the suggestions and I'm still not getting a
redirect to happen.
To simplify matters, I'm using 2 variables.
Any help is appreciated!!!
thanks!

<SCRIPT LANGUAGE="JavaScript">

The language attribute if deprecated in favour of type.

var SoftHard == <%=Session("SoftHard")%>

This should be an assignment (single equals sign) not an equality test
(double equals sign). If the value written into the script from the
Session object is a string then it should be quoted in the JavaScript
source:-

var SoftHard = said:
var OrderNo == <%=Session("OrderNo")%>

Ditto:-

var OrderNo = said:
if SoftHard = "Hard")

Missing opening parenthesis and the if statement's expression should not
be an assignment (single equals sign) it should be a test for equality
(double (or triple) equals sign):-

if(SoftHard == "Hard")
{
strPage = "hardpage.asp?OrderNo=" + OrderNo;
}
else if SoftHard = "Soft")

Ditto:-

else if(SoftHard == "Soft")
{
strPage = "softpage.asp?OrderNo=" + OrderNo;
}
setTimeout("top.location.href = " + strPage,1000);
</SCRIPT>

I can't see any reason for doing this test on the client at all. I don't
know ASP but, guessing at the syntax, what would be wrong with something
like (note: this will line wrap)? :-

<script type="text/javascript">
setTimeout("top.location = \"<%=( (Session("SoftHard") ==
"Soft")?"softpage.asp":"hardpage.asp")%>?OrderNo=<%=Session("OrderNo")%>
\";", 1000);
</script>

Richard.
 
S

shank

Lasse Reichstein Nielsen said:
Should be
<script type="text/javascript">

The type attribute is mandatory, and the language attribute is deprecated.


Should be:
var SoftHard = "<%=Session("SoftHard")%>";

Assignment is single "=".
(I assume the Session function returns a string that is inserted
unquoted, so I added quotes.)


var OrderNo = <%=Session("OrderNo")%>;

(I assumed it was a number, so it didn't need quotes).


should be:
if (SoftHard == "Hard")

Comparison is double "="


Should be:
else if (SoftHard == "Soft")
or just
else
(what else could it be?)


Please don't top post (answering above the message you reply to and
including it unedited below).

/L
------------------------------------
I've added some comments below. Maybe that will help. The help is
outstanding in this group, but I'm still not getting it. In my browser
status bar I get "Done with errors" and of course - no redirect. I changed
one of the variables var OrderNo to OrdNo thinking that maybe that was
messing up the syntax. I'm using OrderNo in the URL string. Any help is
appreciated!!!

<script type="text/javascript">
//Assign 2 variables. Both variables are text
var SoftHard = "<%=Session("SoftHard")%>";
var OrdNo = "<%=Session("OrderNo")%>";
//If SoftHard = Hard then show hardware page
if(SoftHard == "Hard")
{
//Assemble URL for use below
strPage = "hardpage.asp?OrderNo=" + OrdNo; //<--- is this the proper way to
assemble a string
}
//If SoftHard = Soft then show software page
else if(SoftHard == "Soft")
//Assemble URL for use below
{
strPage = "softpage.asp?OrderNo=" + OrdNo; //<--- is this the proper way to
assemble a string
}
//Provide url to get customer back to product page
setTimeout("top.location.href =" + strPage,1000); //<--- is this the proper
way to assemble a string
</script>
 
T

Tim Slattery

shank said:
I'm trying to assemble all the suggestions and I'm still not getting a
redirect to happen.
To simplify matters, I'm using 2 variables.
Any help is appreciated!!!
thanks!

Two equal signs for comparison, one for assignment! And "if"
statements always have to have the logical expression surrounded by
parens.
<SCRIPT LANGUAGE="JavaScript">
var SoftHard == <%=Session("SoftHard")%>

var SoftHard = said:
var OrderNo == <%=Session("OrderNo")%>

var OrderNo = said:
if SoftHard = "Hard")

if (SoftHard == "Hard")
{
strPage = "hardpage.asp?OrderNo=" + OrderNo;
}
else if SoftHard = "Soft")

else if (SoftHard == "Soft")
{
strPage = "softpage.asp?OrderNo=" + OrderNo;
}
setTimeout("top.location.href = " + strPage,1000);
</SCRIPT>
 
S

shank

That's the problem. Still does nothing except show "error on the page" in
the status bar.

Am I correct in thinking that this code can go at the bottom of my page?
thanks
 
R

Richard Cornford

setTimeout("top.location.href =" + strPage,1000);
<snip>

The setTimeout function, when a string argument is its first parameter,
takes a string of JavaScript source code and executes it (approximately)
after the number of milliseconds specified as its second argument.

The code that you want to execute is along the lines of:-

top.location = "stringOfSelectedPage.asp?name=value";

Notice that the value being assigned (right had side of the assignment
operator - = -) is a string; it is surrounded with quote marks.

To pass this line of code to setTimeout the entire line must be
represented as a string. You cannot re-use the double quotes around the
entire string and use then inside the string unless the quotes inside
the string are escaped:-

"top.location = \"stringOfSelectedPage.asp?name=value\";"

- Alternatively single quotes may be used around a string that wants to
treat double quotes literally (or vice versa):-

"top.location = 'stringOfSelectedPage.asp?name=value';"
- or -
'top.location = "stringOfSelectedPage.asp?name=value";'

Now you want to build this string using the - strPage - variable instead
of the URL, it still needs to appear as a quoted string in the
JavaScript code that is the result of the concatenation:-

"top.location = \""+ strPage +"\";"

So the setTimeout call becomes:-

setTimeout( ("top.location = \""+ strPage +"\";"), 1000);

But I still cannot see any reason for doing this on the client as the
server script has all of the information needed to make the decision, so
it only need write out the setTimeout line (and it could write a meta
refresh tag as an alternative to using JavaScript at all).

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
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top