/    Sign up×
Community /Pin to ProfileBookmark

Social Utilities and Javascript Current Page Detection

I’m trying to share personal web pages to any number of the social utilities (i.e. Facebook) out there now. But instead of trying to hard-code every single page url into the “Share to…” code, I was trying to get the page’s url automatically detected and inserted using Javascript. The “Share to…” code and Javascript’s “Detect Current Url” code work correctly independent of each other… I just can’t make them work together. Thoughts?

Below is what I ended up with:

[CODE]
<script type=”text/javascript”>

function currentUrl()
{
return(document.location);
}

function facebook(T, C, U, L)
{
var targetUrl = ‘http://www.facebook.com/sharer.php?u=$document.write(currentUrl())&t=$Title of Page&’ + ‘t=’ + encodeURIComponent(T)
+ ‘&c=’ + encodeURIComponent(C) + ‘&u=’ + encodeURIComponent(U) + ‘&l=’ + L;
window.open(targetUrl);
}

</script>

<a href=”javascript:facebook(‘Testing Post To Facebook’, ‘<p>Testing 1… 2… 3…<i>4… 5…. 6…</i></p>’, ‘document.write(currentUrl())’ &t=$Title of Page&’, 3)”>
<img src=”link-facebook.gif” border=”0″ alt=”Post to Facebook!” /></a>

[/CODE]

“document.write(currentUrl())” inside the “a href” tag seems to be where the whole thing falls apart as actual urls work perfectly.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 09.2008 — The function returns a value; no need for document.write&lt;script type="text/javascript"&gt;
function currentUrl()
{
return(document.location);
}

function facebook(T, C, U, L)
{
var targetUrl = 'http://www.facebook.com/sharer.php?u=$'+currentUrl()+'&amp;t=$Title of Page&amp;' + 't=' + encodeURIComponent(T)
+ '&amp;c=' + encodeURIComponent(C) + '&amp;u=' + encodeURIComponent(U) + '&amp;l=' + L;
window.open(targetUrl);
}

&lt;/script&gt;
&lt;a href="javascript:facebook('Testing Post To Facebook', '&lt;p&gt;Testing 1... 2... 3...&lt;i&gt;4... 5.... 6...&lt;/i&gt;&lt;/p&gt;', currentUrl()+' &amp;t=$Title of Page&amp;', 3)"&gt;
&lt;img src="link-facebook.gif" border="0" alt="Post to Facebook!" /&gt;&lt;/a&gt;
Copy linkTweet thisAlerts:
@antad6authorDec 09.2008 — Thank you, Fang! Works great.

Here's a followup question...

Now I need Javascript to detect each page's title and insert it into the correct places inside the "Share to..." code. So far I have:

<i>
</i>&lt;html&gt;

&lt;head&gt;
&lt;title&gt;test&lt;/title&gt;

&lt;script type="text/javascript"&gt;

function currentUrl()
{
return(document.location);
}

function currentTitle()
{
return (document.title);
}

function myspace(T, C, U, L)
{
var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&amp;'+currentUrl()+ '&amp;t=&amp;' + 't=' + encodeURIComponent(T)
+ '&amp;c=' + encodeURIComponent(C) + '&amp;u=' + encodeURIComponent(U) + '&amp;l=' + L;
window.open(targetUrl);
}

function facebook(T, C, U, L)
{
var targetUrl = 'http://www.facebook.com/sharer.php?u=&amp;'+currentUrl()+ '&amp;t=&amp;' + 't=' + encodeURIComponent(T)
+ '&amp;c=' + encodeURIComponent(C) + '&amp;u=' + encodeURIComponent(U) + '&amp;l=' + L;
window.open(targetUrl);
}
&lt;/script&gt;


&lt;/head&gt;

&lt;body&gt;

&lt;a href="javascript:myspace(' '+currentTitle()+' ','&lt;p&gt;Testing 1... 2... 3...&lt;i&gt;4... 5.... 6...&lt;/i&gt;&lt;/p&gt;', currentUrl()+'&amp;t=&amp;', 3)"&gt;
&lt;img src="http://cms.myspacecdn.com/cms/post_myspace_icon.gif" border="0" alt="Post to MySpace!" /&gt;
&lt;/a&gt;

&lt;a href="javascript:facebook(' '+currentTitle()+' ','&lt;p&gt;Testing 1... 2... 3...&lt;i&gt;4... 5.... 6...&lt;/i&gt;&lt;/p&gt;', currentUrl()+'&amp;t=&amp;', 3)"&gt;
&lt;img src="link-facebook.gif" border="0" alt="Post to Facebook!" /&gt;&lt;/a&gt;

&lt;/body&gt;
&lt;/html&gt;



Myspace returns with an obscure "," at the beginning of the title, and Facebook only returns "www.mywebsite.com" as the title. Thoughts on this?
Copy linkTweet thisAlerts:
@FangDec 10.2008 — myspace(currentTitle(),'&lt;p&gt;Testing 1... 2... 3...&lt;i&gt;4... 5.... 6...&lt;/i&gt;&lt;/p&gt;', currentUrl()+'&amp;t=&amp;', 3)
Copy linkTweet thisAlerts:
@antad6authorDec 17.2008 — I have the same result using your suggestion. What browser were you using to test the code? I've tried IE and Firefox on a PC and Firefox and Safari on a Mac (**prepares for some hate** hehe), all with the same results.
Copy linkTweet thisAlerts:
@FangDec 17.2008 — I'm not [I]au fait[/I] with Facebook; I even had to join-up to see what the script is doing.

It opens Facebook | Share in Post to Profile. Is this the intention?
Copy linkTweet thisAlerts:
@antad6authorDec 17.2008 — Correct, that is the intention. But the web domain of the page's location is inserted as the title of the post instead of what is inside the page's title tags. In this case, "test" should be the title of what is being shared rather than the domain of where the page is located.

MySpace inserts "test" in the correct place on its' submission form, but there is an obscure comma ( , ) that is placed ahead of it.

Sorry that you had to sign up for Facebook. :S
Copy linkTweet thisAlerts:
@LeeUDec 17.2008 — Or, you could just use this little utility and it handles it all. We use it on many of our sites at Jupitermedia.
Copy linkTweet thisAlerts:
@antad6authorDec 19.2008 — Thanks, LeeU ?

I'm not certain I'll use this so please continue posting to this thread if anyone has suggestions for the code above.
Copy linkTweet thisAlerts:
@FangDec 19.2008 — There's a second name/value pair for 't'
function myspace(T, C, U, L)
{
var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&amp;'+currentUrl()+ '&amp;t=' + encodeURIComponent(T)
+ '&amp;c=' + encodeURIComponent(C) + '&amp;u=' + encodeURIComponent(U) + '&amp;l=' + L;
window.open(targetUrl);
}

Sorry that you had to sign up for Facebook. :S[/QUOTE] No problem; I found some distant relatives and old friends while I was there.
Copy linkTweet thisAlerts:
@antad6authorDec 20.2008 — Works perfectly for MySpace! Still only posts the domain as the title for Facebook, though.
×

Success!

Help @antad6 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...