onClick method question (this.href and document.location.href)

Discussion in 'Javascript' started by yogesh.bhardwaj@gmail.com, Feb 3, 2005.

  1. Guest

    Hello all

    I am trying to modify the link URL when a user clicks on the link.
    Following is the code:

    <a onClick="document.location.href=modifyURL(this.href);"
    href="SOME_DYNAMIC_URL" %>Link</a>

    The javascript function, modifyURL(href), works fine and returns the
    desired URL, but for some reason, the value of 'href' is not getting
    overwritten with the value returned from the js function. It works fine
    if I use the following code:

    <a onClick="this.href=modifyURL(this.href);" href="SOME_DYNAMIC_URL"
    %>Link</a>

    But the reason I'm trying to use document.location instead is because
    of an IE bug. The string 'Link' actually contains the character '@', if
    I use this.href, the URL will be formed perfectly, but right before the
    page redirects to the new location, the whole URL will be printed on
    the page (replacing the string 'Link'). This "bug" is overcome by using
    document.location, but then the URL is not getting updated. Any ideas?
    Thanks a lot
    -Yogesh
     
    , Feb 3, 2005
    #1
    1. Advertising

  2. Lee Guest

    said:
    >
    >Hello all
    >
    >I am trying to modify the link URL when a user clicks on the link.
    >Following is the code:
    >
    ><a onClick="document.location.href=modifyURL(this.href);"
    >href="SOME_DYNAMIC_URL" %>Link</a>


    You're telling the browser to do two things that conflict.
    1) set document.location.href to some URL
    2) follow the link to "SOME_DYNAMIC_URL"

    You can cancel action #2 by having the onClick handler return false.

    onclick="document.location.href=modifyURL(this.href);return false"
     
    Lee, Feb 3, 2005
    #2
    1. Advertising

  3. Guest

    Re: onClick method question (this.href and document.location.href)

    Great! that works. Thanks Lee.

    -Yogesh

    Lee wrote:
    > said:
    > >
    > >Hello all
    > >
    > >I am trying to modify the link URL when a user clicks on the link.
    > >Following is the code:
    > >
    > ><a onClick="document.location.href=modifyURL(this.href);"
    > >href="SOME_DYNAMIC_URL" %>Link</a>

    >
    > You're telling the browser to do two things that conflict.
    > 1) set document.location.href to some URL
    > 2) follow the link to "SOME_DYNAMIC_URL"
    >
    > You can cancel action #2 by having the onClick handler return false.
    >
    > onclick="document.location.href=modifyURL(this.href);return false"
     
    , Feb 3, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    200,838
    Adrienne Boswell
    Jun 20, 2006
  2. Chuck

    onclick for button and window.location.href

    Chuck, Feb 2, 2010, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    6,432
    Chuck
    Feb 2, 2010
  3. bob
    Replies:
    3
    Views:
    189
  4. saiho.yuen
    Replies:
    3
    Views:
    437
    kaeli
    Sep 14, 2004
  5. Vincent van Beveren

    BASE HREF and A HREF="#" onclick="..."

    Vincent van Beveren, Jul 6, 2006, in forum: Javascript
    Replies:
    2
    Views:
    330
    Vincent van Beveren
    Jul 6, 2006
Loading...

Share This Page