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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogesh.bhardwaj@gmail.com

    #1

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

    Hello all

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

    <a onClick="docume nt.location.hre f=modifyURL(thi s.href);"
    href="SOME_DYNA MIC_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.h ref=modifyURL(t his.href);" href="SOME_DYNA MIC_URL"
    %>Link</a>

    But the reason I'm trying to use document.locati on 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.locati on, but then the URL is not getting updated. Any ideas?
    Thanks a lot
    -Yogesh

  • Lee

    #2
    Re: onClick method question (this.href and document.locati on.href)

    yogesh.bhardwaj @gmail.com said:[color=blue]
    >
    >Hello all
    >
    >I am trying to modify the link URL when a user clicks on the link.
    >Following is the code:
    >
    ><a onClick="docume nt.location.hre f=modifyURL(thi s.href);"
    >href="SOME_DYN AMIC_URL" %>Link</a>[/color]

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

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

    onclick="docume nt.location.hre f=modifyURL(thi s.href);return false"

    Comment

    • yogesh.bhardwaj@gmail.com

      #3
      Re: onClick method question (this.href and document.locati on.href)

      Great! that works. Thanks Lee.

      -Yogesh

      Lee wrote:[color=blue]
      > yogesh.bhardwaj @gmail.com said:[color=green]
      > >
      > >Hello all
      > >
      > >I am trying to modify the link URL when a user clicks on the link.
      > >Following is the code:
      > >
      > ><a onClick="docume nt.location.hre f=modifyURL(thi s.href);"
      > >href="SOME_DYN AMIC_URL" %>Link</a>[/color]
      >
      > You're telling the browser to do two things that conflict.
      > 1) set document.locati on.href to some URL
      > 2) follow the link to "SOME_DYNAMIC_U RL"
      >
      > You can cancel action #2 by having the onClick handler return false.
      >
      > onclick="docume nt.location.hre f=modifyURL(thi s.href);return false"[/color]

      Comment

      Working...