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

G

Guest

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
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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.
 
G

Guest

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
 
G

Guest

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
 
S

Steven Cheng[MSFT]

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.
 
H

Harshal P

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

in javascript.

Regards,
Harshal P
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top