469,270 Members | 1,037 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

If Then statement

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>
Jul 20 '05 #1
17 5516
Il Mon, 21 Jul 2003 21:35:08 GMT, "shank" <sh***@tampabay.rr.com> ha
scritto nel messaggio
<g0**********************@twister.tampabay.rr.com> :
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
}
--
artemis.alpeia
Jul 20 '05 #2
"shank" <sh***@tampabay.rr.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
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>

"artemis.alpeia" <it*******@alpeia.artemis> wrote in message
news:5l********************************@4ax.com...
Il Mon, 21 Jul 2003 21:35:08 GMT, "shank" <sh***@tampabay.rr.com> ha
scritto nel messaggio
<g0**********************@twister.tampabay.rr.com> :
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
}
--
artemis.alpeia

Jul 20 '05 #4
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" <lr*@hotpop.com> wrote in message
news:3c**********@hotpop.com...
"shank" <sh***@tampabay.rr.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #5
> 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

Jul 20 '05 #6
"shank" <sh***@tampabay.rr.com> wrote in message
news:DO**********************@twister.tampabay.rr. com...
Still not getting it right. Am I incorporating my
Session("OrderNo") variable correctly? <snip> 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.
Jul 20 '05 #7
shank wrote:
I'm coming from the ASP worl and have no clue to javascript.


Mate, you've got some reading to do:
http://devedge.netscape.com/central/javascript/
http://www.mozilla.org/js/scripting/
http://gemal.dk/mozilla/mozdev.html

Jul 20 '05 #8
"shank" <sh***@tampabay.rr.com> wrote:
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}
setTimeout("top.location.href = strPage",4000);
</SCRIPT>

--
Tim Slattery
Sl********@bls.gov
Jul 20 '05 #9
"shank" <sh***@tampabay.rr.com> wrote in message
news:g0**********************@twister.tampabay.rr. com...
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?

Jul 20 '05 #10
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!

"Steven Dilley" <st***********@compuware.com> wrote in message
news:3f******@10.10.0.241...
"shank" <sh***@tampabay.rr.com> wrote in message
news:g0**********************@twister.tampabay.rr. com...
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?

Jul 20 '05 #11
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>
"Tim Slattery" <Sl********@bls.gov> wrote in message
news:b9********************************@4ax.com...
"shank" <sh***@tampabay.rr.com> wrote:
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!


Not directly. You'll have to write the value of the server-side
variable into the client-side script. Something like this:

<SCRIPT LANGUAGE="JavaScript">
if ("<%=Session("SoftHard")%>" == "Hard")
{
/* whatever */
.
.
.

The value of the Session variable "SoftHard" is written to the client
as part of the client-side JavaScript code. The client sees this
(assuming the value of SoftHard is "Soft"):

<SCRIPT LANGUAGE="JavaScript">
if ("Soft" == "Hard")
{
/* whatever */
.
.
.

--
Tim Slattery
Sl********@bls.gov

Jul 20 '05 #12
"shank" <sh***@tampabay.rr.com> wrote in message
news:hL***************@twister.tampabay.rr.com...
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.

<script type="text/javascript">
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 = "<%=Session("SoftHard")%>";
var OrderNo == <%=Session("OrderNo")%>
Ditto:-

var OrderNo = "<%=Session("OrderNo")%>";
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.
Jul 20 '05 #13

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:y8**********@hotpop.com...
"shank" <sh***@tampabay.rr.com> writes:
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!!!

<SCRIPT LANGUAGE="JavaScript">


Should be
<script type="text/javascript">

The type attribute is mandatory, and the language attribute is deprecated.
var SoftHard == <%=Session("SoftHard")%>


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")%>


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

(I assumed it was a number, so it didn't need quotes).
if SoftHard = "Hard")


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

Comparison is double "="
{
strPage = "hardpage.asp?OrderNo=" + OrderNo;
}
else if SoftHard = "Soft")


Should be:
else if (SoftHard == "Soft")
or just
else
(what else could it be?)
{
strPage = "softpage.asp?OrderNo=" + OrderNo;
}
setTimeout("top.location.href = " + strPage,1000);
</SCRIPT>


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

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

------------------------------------
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>
Jul 20 '05 #14
"shank" <sh***@tampabay.rr.com> wrote:
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 = "<%=Session("SoftHard")%>"
var OrderNo == <%=Session("OrderNo")%>
var OrderNo = "<%=Session("OrderNo")%>"
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>
"Tim Slattery" <Sl********@bls.gov> wrote in message
news:b9********************************@4ax.com.. .
"shank" <sh***@tampabay.rr.com> wrote:
>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!


Not directly. You'll have to write the value of the server-side
variable into the client-side script. Something like this:

<SCRIPT LANGUAGE="JavaScript">
if ("<%=Session("SoftHard")%>" == "Hard")
{
/* whatever */
.
.
.

The value of the Session variable "SoftHard" is written to the client
as part of the client-side JavaScript code. The client sees this
(assuming the value of SoftHard is "Soft"):

<SCRIPT LANGUAGE="JavaScript">
if ("Soft" == "Hard")
{
/* whatever */
.
.
.

--
Tim Slattery
Sl********@bls.gov

--
Tim Slattery
Sl********@bls.gov
Jul 20 '05 #15
"shank" <sh***@tampabay.rr.com> wrote:
Two equal signs for comparison, one for assignment! And "if"
statements always have to have the logical expression surrounded by
parens.


Yup, looks fine now.

--
Tim Slattery
Sl********@bls.gov
Jul 20 '05 #16
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

"Tim Slattery" <Sl********@bls.gov> wrote in message
news:ek********************************@4ax.com...
"shank" <sh***@tampabay.rr.com> wrote:
Two equal signs for comparison, one for assignment! And "if"
statements always have to have the logical expression surrounded by
parens.


Yup, looks fine now.

--
Tim Slattery
Sl********@bls.gov

Jul 20 '05 #17
"shank" <sh***@tampabay.rr.com> wrote in message
news:zX*****************@twister.tampabay.rr.com.. .
<snip>
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.
Jul 20 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Fábio Mendes | last post: by
13 posts views Thread by eman1000 | last post: by
37 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by Steven Bethard | last post: by
28 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by dspfun | last post: by
23 posts views Thread by florian.loitsch | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.