Assigning the URL in the anchor tag.using javascript

Posted by Rajass22 under ASP.NET on 8/28/2013 | Points: 10 | Views : 2098 | Status : [Member] | Replies : 8
Hi,

My problem is on the page loads i want to assign the url in the anchor tag like below. But it not working at all. I tried in both a way with and without runat server.

window.onload = function () {

document.getElementById('placeholder').href = 'drawPicture.aspx?code=MIL-2653-22CT&width = 550';

}

<a id="placeholder" rel="zoom-width:600px; zoom-height:600px" class="MagicZoom">

But it work's fine when i put the aboove URL directly like below

<a id="placeholder" rel="zoom-width:600px; zoom-height:600px" href="drawPicture.aspx?code=MIL-2653-22CT&width = 550" runat="server" class="MagicZoom">

But i want to be done by like " document.getElementById('placeholder').href = 'drawPicture.aspx?code=MIL-2653-22CT&width = 550';"

Please help !!




Responses

Posted by: Bandi on: 8/28/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://stackoverflow.com/questions/3998496/change-the-href-of-an-anchor-tag-with-javascript?rq=1

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 8/28/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
I have checked you code.
Please change the code as below:-

<script type="text/javascript" language="javascript">
window.onload = function () {
document.getElementById('placeholder').href = 'drawPicture.aspx?code=MIL-2653-22CT&width = 550';
}
</script>

<a id="placeholder" rel="zoom-width:600px; zoom-height:600px" href="" class="MagicZoom">test</a>

Also if you are using master page on your page then,

<script type="text/javascript" language="javascript">
window.onload = function () {
document.getElementById('#<%= placeholder.ClientID %>').href = 'drawPicture.aspx?code=MIL-2653-22CT&width = 550';
}
</script>

<a id="placeholder" rel="zoom-width:600px; zoom-height:600px" href="" class="MagicZoom" runat="server">test</a>


Happy coding.

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajass22 on: 8/28/2013 [Member] Starter | Points: 25

Up
0
Down
Allemahesh,

I am using master page. I have tried your code but it's not working.

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 8/28/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Raja,

Here is the code:-
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="Server">
<script type="text/javascript" language="javascript">
window.onload = function () {
document.getElementById('placeholder').href = 'drawPicture.aspx?code=MIL-2653-22CT&width = 550';
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<a id="placeholder" rel="zoom-width:600px; zoom-height:600px" href="" class="MagicZoom">
test</a>
</asp:Content>

Happy coding

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajass22 on: 8/28/2013 [Member] Starter | Points: 25

Up
0
Down
Allemahesh,

No it's not working.

Here is my code

<script type="text/javascript" language="javascript">
window.onload = function () {
document.getElementById('placeholder').href = 'drawPicture.aspx?code=MIL-2653-22CT&width = 550';
}

<span>

<a id="placeholder" rel="zoom-width:600px; zoom-height:600px" href="" class="MagicZoom">

<asp:Image ID="ImageButton2" BorderWidth="1px" BorderColor="LightGray" runat="server" onerror="this.onerror=null;this.src='../Image/noimage2.png'" Width="250px" /></a>

</span>

I am using it for zooming. When it same thing is given directly in anchor tag means it's work fine. But i want to assign it on page loads using java script.

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 8/28/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Raja,
I have checked on my PC and it is working fine. Can you tell me which browser you are checking.
happy Coding.

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajass22 on: 8/28/2013 [Member] Starter | Points: 25

Up
0
Down
I am checking using Chrome browser

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nitesh.Luharuka on: 8/28/2013 [Member] Starter | Points: 25

Up
0
Down
You can try using jquery $(documnet).ready() to set the href parameter.

Try checking using firebug in firefox or object browser in chrome to see whether your code is throwing an error or not.

Nitesh Luharuka
http://www.niteshluharuka.com

Rajass22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response