From: menkaur on
Hey, guys!

I'm new to javascript, and i've got a problem with one of the simple
functions.

I am trying to redirect a user after he presses a button. Here is the
code:
<form method="post"
name="form2"
action="">
<center>
<label><input id="getRecomendation"
value="Get my recomendation"
type="submit"
name="getRecomendation"
onclick="giveRecomendation()">
<script type="text/javascript">
function giveRecomendation()
{
window.location = "foo.html";
//alert(window.location);
}

the page is run in Firefox from local hard drive.
if alert(window.location) is uncommented, everything works just fine.
but if i comment it, nothing happens.

what's wrong?
From: David Mark on
On Nov 28, 5:24 am, menkaur <menk...(a)gmail.com> wrote:
> Hey, guys!
>
> I'm new to javascript, and i've got a problem with one of the simple
> functions.
>
> I am trying to redirect a user after he presses a button. Here is the
> code:
> <form method="post"
>       name="form2"
>       action="">
>     <center>

Lose that (all of it).

>         <label><input id="getRecomendation"

Validate your markup.

>                value="Get my recomendation"
>                type="submit"

type="button"

>                name="getRecomendation"
>                onclick="giveRecomendation()">
> <script type="text/javascript">
>   function giveRecomendation()
>   {
>       window.location = "foo.html";

window.location.href = "foo.html";

>       //alert(window.location);

//window.alert(window.location.href);

>
> }
>

But this button won't work without scripting (and why are you using a
button for a link anyway?) Good way to lose out on recommendations
from people who can't/won't enable JS on the Internet. You could use
a form and a submit button, but you would need to set the action to
foo.html. That wouldn't require any script, but neither would a
standard link.
From: menkaur on
On Nov 28, 1:02 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
> On Nov 28, 5:24 am, menkaur <menk...(a)gmail.com> wrote:
>
> > Hey, guys!
>
> > I'm new to javascript, and i've got a problem with one of the simple
> > functions.
>
> > I am trying to redirect a user after he presses a button. Here is the
> > code:
> > <form method="post"
> >       name="form2"
> >       action="">
> >     <center>
>
> Lose that (all of it).
>
> >         <label><input id="getRecomendation"
>
> Validate your markup.
>
> >                value="Get my recomendation"
> >                type="submit"
>
> type="button"
>
> >                name="getRecomendation"
> >                onclick="giveRecomendation()">
> > <script type="text/javascript">
> >   function giveRecomendation()
> >   {
> >       window.location = "foo.html";
>
> window.location.href = "foo.html";
>
> >       //alert(window.location);
>
> //window.alert(window.location.href);
>
>
>
> > }
>
> But this button won't work without scripting (and why are you using a
> button for a link anyway?)  Good way to lose out on recommendations
> from people who can't/won't enable JS on the Internet.  You could use
> a form and a submit button, but you would need to set the action to
> foo.html.  That wouldn't require any script, but neither would a
> standard link.

I'm using a button, because i'd like the customer to select options,
and, based on selected options redirect the customer to an appropriate
page.
From: Asen Bozhilov on
Because internal setter of window.location will be execute
asynchronously, maybe in different thread. See the example:

window.location.href = 'http://google.com';
var arr = [];
for (var i = 0; i < 10; i++)
{
arr[i] = i;
}
window.alert(arr);

Unfortunately, I don't see any documentation about that behavior of
window.location to read what exactly happens in internal setter of
window.location.

In your case, you should need to prevent default action of submit
button. Because, before created HTTP request from internal setter of
window.location, will be make HTTP/POST request to URL from action
attribute of form object.

From: David Mark on
On Nov 28, 6:07 am, menkaur <menk...(a)gmail.com> wrote:
> On Nov 28, 1:02 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
>
>
>
> > On Nov 28, 5:24 am, menkaur <menk...(a)gmail.com> wrote:
>
> > > Hey, guys!
>
> > > I'm new to javascript, and i've got a problem with one of the simple
> > > functions.
>
> > > I am trying to redirect a user after he presses a button. Here is the
> > > code:
> > > <form method="post"
> > >       name="form2"
> > >       action="">
> > >     <center>
>
> > Lose that (all of it).
>
> > >         <label><input id="getRecomendation"
>
> > Validate your markup.
>
> > >                value="Get my recomendation"
> > >                type="submit"
>
> > type="button"
>
> > >                name="getRecomendation"
> > >                onclick="giveRecomendation()">
> > > <script type="text/javascript">
> > >   function giveRecomendation()
> > >   {
> > >       window.location = "foo.html";
>
> > window.location.href = "foo.html";
>
> > >       //alert(window.location);
>
> > //window.alert(window.location.href);
>
> > > }
>
> > But this button won't work without scripting (and why are you using a
> > button for a link anyway?)  Good way to lose out on recommendations
> > from people who can't/won't enable JS on the Internet.  You could use
> > a form and a submit button, but you would need to set the action to
> > foo.html.  That wouldn't require any script, but neither would a
> > standard link.
>
> I'm using a button, because i'd like the customer to select options,
> and, based on selected options redirect the customer to an appropriate
> page.

You should use a form and do the redirect on the server. No JS
necessary.