views:

253

answers:

4

Howdy,

My brain is fried ATM and I have a looming deadline, I am looking for a way to grab the currently viewed pages url and place it into another link for bookmarking/social links.

Facebook link:http://www.facebook.com/share.php?u=[PAGEURL]
Myspace link:http://www.myspace.com/Modules/PostTo/Pages/?u=[PAGEURL]
Twitter link: http://twitter.com/home?status=Check+this+out:+[PAGEURL]
Stumble Upon link:http://www.stumbleupon.com/submit?url=[PAGEURL]
Digg link: http://digg.com/submit?phase=2&url=[PAGEURL]

I need to replace [PAGEURL] with the URL for the page being viewed. Any help is most appreciate. i have been looking for a while now and can't seems to find an answer that fits this specific circumstance.

+1  A: 

window.location.href should work.

Jacob Relkin
+1  A: 

I imagine it would be something like

var faceBookUrl = "http://www.facebook.com/share.php?u=" + location.href
stimms
+2  A: 

It'd help to see what kind of structure those links are in. But, here's some jQuery that might point you in a good direction. It assumes your social bookmarking links are in a container with id="socials", but you can mash the selector to do whatever it takes to get hold of your social links.

$(function() {
    var links = $('#socials a'),
        matchExp = /\[PAGEURL\]/,
        currentURL = location.href;

    links.each(function() {
        var currentHREF = $(this).attr('href');
        if (currentHREF.match(matchExp)) {
            $(this).attr('href',currentHREF.replace(matchExp,currentURL));
        }
    });

});

This uses the attr function to get where the link points to, then a regular expression (eww!) to check if the link has [PAGEURL] in it, and to replace [PAGEURL] with location.href, which is the url of the current page. Here's a handy regexp tester.

Dan F
Wow Dan Thanks! I think this is the right direction. I'll try this out right now.
Jamie
The Bomb! Thanks so much perfect!
Jamie
No worries, glad it helped! If it's not too much trouble, could you click the green tick and mark this as the accepted answer? This gives us both a few extra points - see http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about for more details. Cheers!
Dan F
A: 

Thanks Dan that was just the bit I needed. Muchas Gracias!

Jamie