how do I change the href property to the web anchor element

Discussion in 'ASP .Net' started by =?Utf-8?B?ZXN3YW5zb24=?=, Mar 26, 2006.

  1. In jscript, I would like to be able to set the href attribute of the anchor
    element. I have a aspnet:hyperlink button on the page, but I would like to
    be able to set the href attribute of this element via jscript.

    How do I do this?

    I am attempting to prevent extra trips to the server. Currently, I set the
    navigateurl in server side scripting, but I know the answer on the client.

    Thanks

    Eric
    =?Utf-8?B?ZXN3YW5zb24=?=, Mar 26, 2006
    #1
    1. Advertising

  2. javascript
    On the server you build the clientid of the anchor right into the script or
    element event that changes it.

    dim x as new HTMLAnchor
    controls.add(x)
    dim chkItalic As New CheckBox
    chkItalic.Text = "Change Href"
    chkItalic.InputAttributes.Add("onclick", "changehref(""" & x.ClientID & """)")
    Controls.Add(chkItalic)

    <script>
    function changehref(k23er) {
    document.getElementById(k23er).href="helloworld.aspx";
    }
    </script>

    Good Luck
    DWS

    "eswanson" wrote:

    > In jscript, I would like to be able to set the href attribute of the anchor
    > element. I have a aspnet:hyperlink button on the page, but I would like to
    > be able to set the href attribute of this element via jscript.
    >
    > How do I do this?
    >
    > I am attempting to prevent extra trips to the server. Currently, I set the
    > navigateurl in server side scripting, but I know the answer on the client.
    >
    > Thanks
    >
    > Eric
    =?Utf-8?B?RFdT?=, Mar 27, 2006
    #2
    1. Advertising

  3. Hi Eric,

    As DWS has mentioend, if you have explicitly assigned an ID for the html
    anchor, we can use the document.getElementById method to reference it in
    client-side script.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Mar 27, 2006
    #3
  4. Hi Steven,

    On the client side, I attempted to use setAttribute("href","www.msn.com");
    and it appeared to fail.

    Is there a difference between .href and .setAttribute?

    I will try this other method.

    Thanks

    Eric

    "Steven Cheng[MSFT]" wrote:

    > Hi Eric,
    >
    > As DWS has mentioend, if you have explicitly assigned an ID for the html
    > anchor, we can use the document.getElementById method to reference it in
    > client-side script.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    =?Utf-8?B?ZXN3YW5zb24=?=, Mar 27, 2006
    #4
  5. I did the suggested command from jscript on the client, but I have to put an
    alert box for the screen to see my button changes.

    Here is my script:

    document.getElementById("cmdViewPDF").href = "/PDF/"+
    newRow.getCell(5).getValue();
    alert("set href");

    Only after the alert is fired does my anchor show up changed. If I remove
    the alert statement, the href does not get updated.

    How do I refresh the screen without the alert statement?

    Thanks

    Eric

    "eswanson" wrote:

    > Hi Steven,
    >
    > On the client side, I attempted to use setAttribute("href","www.msn.com");
    > and it appeared to fail.
    >
    > Is there a difference between .href and .setAttribute?
    >
    > I will try this other method.
    >
    > Thanks
    >
    > Eric
    >
    > "Steven Cheng[MSFT]" wrote:
    >
    > > Hi Eric,
    > >
    > > As DWS has mentioend, if you have explicitly assigned an ID for the html
    > > anchor, we can use the document.getElementById method to reference it in
    > > client-side script.
    > >
    > > Regards,
    > >
    > > Steven Cheng
    > > Microsoft Online Community Support
    > >
    > >
    > > ==================================================
    > >
    > > When responding to posts, please "Reply to Group" via your newsreader so
    > > that others may learn and benefit from your issue.
    > >
    > > ==================================================
    > >
    > >
    > > This posting is provided "AS IS" with no warranties, and confers no rights.
    > >
    > >
    > >
    > >
    =?Utf-8?B?ZXN3YW5zb24=?=, Mar 27, 2006
    #5
  6. Hi Eric,

    setAttribute/getAttribute is mainly used for XML document since they're the
    standard interfaces defined by the W3C DOM specification. For html
    element's certain property, we should always use their name to directly
    reference them. And for the new problem you mentioned, are you going to
    change the displayed text or the href address of the hyperlink control
    ,also where do you put the script code in your page's client-side html
    source, would you provide some detailed code snippet of the page?

    here is my test page's clent-side code which change a hyperlink's title and
    location:

    =======================================
    .....................

    <script language="javascript">
    function change_link()
    {
    var link = document.getElementById("HyperLink1");

    link.href = "http://www.asp.net";
    link.title = "ASP.NET Site";
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <input type="file" id="file1" name="file1" runat="server"
    onchange="file_onchange(this);" />

    <a id="link" href="." >my hyperlink</a>

    ......................
    ==============================

    Hope this helps.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Mar 28, 2006
    #6
  7. =?Utf-8?B?ZXN3YW5zb24=?=

    Harshal P Guest

    use
    window.document.getElementById('<id of the element>').href = "new href
    value";

    in javascript.

    Regards,
    Harshal P

    "eswanson" <> wrote in message
    news:...
    > In jscript, I would like to be able to set the href attribute of the
    > anchor
    > element. I have a aspnet:hyperlink button on the page, but I would like
    > to
    > be able to set the href attribute of this element via jscript.
    >
    > How do I do this?
    >
    > I am attempting to prevent extra trips to the server. Currently, I set
    > the
    > navigateurl in server side scripting, but I know the answer on the client.
    >
    > Thanks
    >
    > Eric
    Harshal P, Mar 28, 2006
    #7
    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. Steve Richter

    anchor href as relative path

    Steve Richter, May 3, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    830
    Steve Richter
    May 3, 2005
  2. Soren Vejrum
    Replies:
    4
    Views:
    514
    Lasse Reichstein Nielsen
    Jul 5, 2003
  3. Johnny
    Replies:
    4
    Views:
    129
    kaeli
    Jan 19, 2004
  4. Replies:
    14
    Views:
    228
    Bart Lateur
    Oct 26, 2006
  5. delimiter

    Change href with @ in anchor text

    delimiter, Sep 19, 2007, in forum: Javascript
    Replies:
    3
    Views:
    154
    delimiter
    Sep 19, 2007
Loading...

Share This Page