Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > window.location doesn't seem to work

Reply
Thread Tools

window.location doesn't seem to work

 
 
menkaur
Guest
Posts: n/a
 
      11-28-2009
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?
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      11-28-2009
On Nov 28, 5:24*am, menkaur <menk...@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.
 
Reply With Quote
 
 
 
 
menkaur
Guest
Posts: n/a
 
      11-28-2009
On Nov 28, 1:02*pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Nov 28, 5:24*am, menkaur <menk...@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.
 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      11-28-2009
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.

 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-28-2009
On Nov 28, 6:07*am, menkaur <menk...@gmail.com> wrote:
> On Nov 28, 1:02*pm, David Mark <dmark.cins...@gmail.com> wrote:
>
>
>
> > On Nov 28, 5:24*am, menkaur <menk...@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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-28-2009
David Mark wrote:

> menkaur wrote:
>> <label><input id="getRecomendation"

>
> Validate your markup.


ACK, but this line is not invalid. <http://validator.w3.org/>

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

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


There is no inherent advantage in using `.location.href' over `.location'.
In fact, by contrast the `href' property of Location instances is tainted,
and so subject to the SOP (that does not matter here).


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-28-2009
Asen Bozhilov wrote:

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


<http://docs.sun.com/source/816-6408-10/window.htm#1202507>
<http://docs.sun.com/source/816-6408-10/location.htm#1193137>

> In your case, you should need to prevent default action of submit
> button.


No, the `submit' event of the form should be cancelled instead. Or a submit
button should not be used in the first place, or scripting should not be
used in the first place.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      11-28-2009
Thomas 'PointedEars' Lahn wrote:

> <http://docs.sun.com/source/816-6408-10/window.htm#1202507>
> <http://docs.sun.com/source/816-6408-10/location.htm#1193137>


Thanks for the links. They contains much more information from MDC,
but again i don't see any explanations about, how internal setter of
window.location works.

> No, the `submit' event of the form should be cancelled instead. *

Yes, yes, yes. Apologize for my terminology here.

Nice to see, your correction again. Sometimes you are make ironical
comment, but when you corrected my, i learn properly terminology.
Thanks.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-28-2009
Asen Bozhilov wrote:

> Thomas 'PointedEars' Lahn wrote:
>> <http://docs.sun.com/source/816-6408-10/window.htm#1202507>
>> <http://docs.sun.com/source/816-6408-10/location.htm#1193137>

>
> Thanks for the links. They contains much more information from MDC,
> but again i don't see any explanations about, how internal setter of
> window.location works.


,-<http://docs.sun.com/source/816-6408-10/location.htm>
|
| Description
| [...]
| If you assign a string to the location property of an object, JavaScript
| creates a location object and assigns that string to its href property.

See also <http://docs.sun.com/source/816-6408-10/location.htm#1193550>:

| Description
|
| [...]
| Omitting a property name from the location object is equivalent to
| specifying location.href. [...]

RTSL of the respective DOM implementation, if available, for details.

>> No, the `submit' event of the form should be cancelled instead.

> Yes, yes, yes. Apologize for my terminology here.
>
> Nice to see, your correction again. Sometimes you are make ironical
> comment, but when you corrected my, i learn properly terminology.


It is not merely a matter of terminology here, though.

Preventing the default action of (or cancelling) the `click' event of the
submit button means, in a nutshell:

<input type="submit" ... onclick="return false">

Cancelling (or preventing the default action of) the `submit' event of the
form means, in a nutshell

<form ... onsubmit="return false">

The former approach is known to be error-prone and harder to maintain.

> Thanks.


You are welcome.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NAT doesn't seem to work on all ports gqmetro@yahoo.com Cisco 1 06-15-2005 08:26 AM
router doesn't seem to work =?Utf-8?B?ZA==?= Wireless Networking 2 06-12-2005 06:45 PM
Can't seem to get debugger to work tfs ASP .Net 1 06-28-2004 09:49 PM
Error downloading page, some pages work great but cant seem to get this one Jack Schafer Perl 1 04-23-2004 08:32 AM
&nbsp; in a ListItem doesn't seem to work Bill Green ASP .Net 2 02-07-2004 01:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57