Cunners
Cunners

Reputation: 1286

How do I add custom hyperlinks to a webpage

I want to add a hyperlink to a social bookmarking site om my webpage which requires me to include the name of the page it is being sent from.

Example of what I am trying to achieve:

Current page is:

http://www.testpage.com/testpage.aspx?test=34

Hyperlink I want to create on the above page:

http://www.stumbleupon.com/submit?url=http://www.testpage.com/testpage.aspx?test=34

What is the easiest way to programmatically add this customised hyperlink to a webpage?

Upvotes: 0

Views: 488

Answers (3)

Canavar
Canavar

Reputation: 48098

Assuming that you have a hyperlink like that :

<asp:HyperLink runat="server" ID="myLink" Text="stumbleupon"></asp:HyperLink>

At server side :

string currentPagesUrl = 
       HttpUtility.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri);
myLink.NavigateUrl = string.Format("http://www.stumbleupon.com/submit?url={0}",  
    currentPagesUrl);

Or an alternative way (this one is easier I think) :

<a href="http://www.stumbleupon.com/submit?url=<%= HttpUtility.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri) %>" target="_blank">
    stumbleupon 2</a>

Upvotes: 2

bernhof
bernhof

Reputation: 6330

I second Canavar's answer. You might want to also URL encode the currentPagesUrl string when building the hyperlink:

myLink.NavigateUrl = string.Format("http://www.stumbleupon.com/submit?url={0}",
    Server.UrlEncode(currentPagesUrl));

Upvotes: 2

stefanw
stefanw

Reputation: 10570

Using jQuery:

$(document).ready(function(){
    $("a.stumblethis").each(function(){
          $(this).attr("href", "http://www.stumbleupon.com/submit?url="+$(this).attr("href"));
        });    
});

This will convert all links that have a class of "stumblethis".

Upvotes: 1

Related Questions