[Javascript] link onclick=location.href not working

Lau lau at goldenweb.com.au
Tue Mar 30 21:10:31 CST 2004


I found the solution. Just put the javascript in the HREF instead of in the
onclick.
This is what I tried initially but I must have screwed something up because
it didn't work at all so I resorted to putting it in the onclick.
I gave it another go in the HREF and this time it worked!

Thanks for all your other suggestions like using the onclick of the button
itself but I can't use them in my situation as I'm writing an application
that allows non-technical people to build their own e-commerce site and I
need to be able to let them insert a link to the "Add to cart" anywhere on a
page. Some may want to use a button, some an image or some just plain text
that says "buy".
When the customer clicks that "Add to cart" link I need to execute a
javascript function that checks whether this product has any dropdown
options such as colour and size and if so add the select options as
parameters to the url that adds the product to the cart.

Thanks anyway.
Peace
Lau


-----Original Message-----
From: javascript-bounces at LaTech.edu [mailto:javascript-bounces at LaTech.edu]On
Behalf Of David Lovering
Sent: Tuesday, 30 March 2004 11:09 PM
To: [JavaScript List]
Subject: Re: [Javascript] link onclick=location.href not working

That's very odd -- the structure I mean, not the problem.  The way you've
chosen to do this strikes me as a bit bizarre -- the fact that IE6 does it
this way is a testimony to the screwy things it does support.

It looks as though you are indexing buttons, which when clicked change the
window.location.href to something else.  I usually choose to use frames
(shouts and cries of protest from the non-frame crowd), putting all my
non-moving buttons in one frame which doesn't scroll.  No indexing required.
If you MUST put an index around a button, I'd do it inside a DIV so that the
event-handling logic is a bit more straight-forward.  [Not a requirement of
W3C -- just my own programming eccentricity].

Secondly, I'd attach the onclick event to the button itself -- not the index
(which by the above reasoning becomes unnecessary).
A real hard-core standards type would suggest prefacing the javascript
directive reassigning the window.location.href with "javascript:", but I'm
not him.

The other way to modularize the whole thing (whichever method you eventually
turn to) is to put the href changer into a function outside the
link/button(s), so that if you change the mechanism later you only have to
do it one place.  Yes, I know that brain-dead defective Micro$oft-style
page-generation tools (Frontpage, etc.) sometimes replicate the intrinsic
active script in each object, but that's just silly and shouldn't be taken
as an example of how to do things CORRECTLY.

Ultimately, if you want to go to the "next level" of modularity, you can
largely automate the whole process by creating an entirely new object (an
"indexPageChange" perhaps?) which automatically counts the other incidences
of "indexPageChange" objects (or whatever), bumps the counter by one, uses
that to define the index number used in the link, and correctly references
the href changer function with the proper link URL.  This is probably
somewhat over-the-top for your needs, but may be worth considering in the
future if you plan to do this sort of thing a lot.  With one include (the
function library and object definitions), you could then reference as many
indexPageChange objects as you liked, just using them as wrappers for the
URL(s) they used. You could even (horrors!) add the URL's to an array, so
that the working code only references the link address by the index itself,
without actually including the fully hairy-chested URL string in the call.
That way you could validate the URL for forbidden characters, syntax, etc.
outside the link invocation -- always a good idea in these
security-conscious times.

At least, that is my opinion.

-- Dave Lovering


----- Original Message -----
From: "Lau at goldenweb.com.au" <lau at goldenweb.com.au>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Tuesday, March 30, 2004 2:11 AM
Subject: [Javascript] link onclick=location.href not working


> I'm having trouble changing the window.location.href from the onclick
event
> of a link. using IE5.0
> It works if the link is wrapped around a button but if its wrapped around
> text or an image.
>
> This works
> <A HREF='#aa' onclick='window.location.href =
> "http://www.google.com";'><INPUT TYPE=BUTTON VALUE="BUY"></A>
>
> but this doesn't
> <A HREF='#aa' onclick='window.location.href =
> "http://www.google.com";'>aaaa</A>
>
> In my actual code I have a function in the onclick that determines the URL
> to assign.
>
> It works in IE6.0 but I need to get it working in all browsers.
>
> Could you please test this on your version browser and let me know if it
> works or not?
>
> And what workaround can I use.
>
> Thanks in advance
> Lau
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>


_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list