tags:

views:

143

answers:

3

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?

A: 

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

stefanw
Cost a lot (looping through every node in document), and doesn't work with client that doesn't support javascript, that's why you get my -1
Clement Herreman
+2  A: 

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=&lt;%= HttpUtility.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri) %>" target="_blank">
    stumbleupon 2</a>
Canavar
Shouldn't HttpContext.Current.Request.Url.AbsoluteUri be url encoded?
JohannesH
yes you're right, I forget it, adding now. Thanks !
Canavar
worked like a charm!! Thanks.
Cunners
+2  A: 
Bernhof