Forum Moderators: open
<a href="javascript: document.write('http://www.linkedin.com/shareArticle?mini=true&url=' + location.href);"
title="Share this page on LinkedIn">Share</a>
<ul class="sharing">
<li id="facebook">...</li>
<li id="linkedin"><a href="#">Share on LinkedIn</a></li>
<li id="twitter">...</li>
</ul>
document.getElementById('linkedin').getElementsByTagName('a')[0]
function setLinkDestinations() {
document.getElementById('linkedin').getElementsByTagName('a')[0].setAttribute('href','http://www.linkedin.com/shareArticle?mini=true&url=' + location.href);}
var sharingNetworks = ['facebook', 'linkedin', 'twitter'];
var sharingNetworkLinks = {facebook:'URL here', linkedin:'http://www.linkedin.com/shareArticle?mini=true&url=', twitter:'URL here'};
getElementById('') [edited by: ronin at 8:18 pm (utc) on Nov 2, 2014]
function setLinkDestinations() {
document.getElementById('linkedin').getElementsByTagName('a')[0].setAttribute('href','http://www.linkedin.com/shareArticle?mini=true&url=' + location.href);}
{
document.getElementById('facebook').getElementsByTagName('a')[0].setAttribute('href','http://www.facebook.com/sharer.php?u=' + location.href);}
change the inline javascript into unobtrusive javascript (so you can separate structure and function
I was trying to keep everything inline intentionally, as there are only 2 sharing links and it seems simpler and tidier that way.
<div class="share-links"></div>
(function () {
var i,
n,
placeholders;
try {
placeholders = document.getElementsByClassName('share-links');
}
catch (e) {
// getElementsByClassName is not supported by IE <= 8
// You can add a polyfill for those obsolete versions if you need it
return;
}
function appendLinks(el) {
// append links to el
}
for (i = 0, n = placeholders.length; i < n; i++) {
appendLinks(placeholders[i]);
}
})();
function createLinkedInShareLink() {
var el = document.createElement('a');
el.href = 'http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(location.href);
el.title = 'Share this page on LinkedIn';
el.appendChild(document.createTextNode('Share'));
return el;
}
function appendLinks(el) {
// append links to el
el.appendChild(createLinkedInShareLink());
}
(function () {
var i,
n,
placeholders;
try {
placeholders = document.getElementsByClassName('share-links');
}
catch (e) {
// getElementsByClassName is not supported by IE <= 8
// You can add a polyfill for those obsolete versions if you need it
return;
}
function createLinkedInShareLink() {
var el = document.createElement('a');
el.href = 'http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(location.href);
el.title = 'Share this page on LinkedIn';
el.appendChild(document.createTextNode('Share'));
return el;
}
function appendLinks(el) {
// append links to el
el.appendChild(createLinkedInShareLink());
}
for (i = 0, n = placeholders.length; i < n; i++) {
appendLinks(placeholders[i]);
}
})();
_paq.push(['trackEvent','Newsletter','Subscribe',location.pathname.substring(1)])
Would it not be better to use getElementsById? Or is this do with the 2 different services?
when I saw that getElementsByClassName wasn't supported by IE<8 I thought it might work better, but getElementById seems to have the same problem...
(function () {
$('.share-links').each(function () {
$(this).append(createLinkedInShareLink())
.append(" ")
.append(createFacebookShareLink());
});
function createLinkedInShareLink() {
return $("<a />", {
href: 'http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(location.href),
title: 'Share this page on LinkedIn',
text: 'Share'
});
}
function createFacebookShareLink() {
return $("<a />", {
href: 'http://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(location.href),
title: 'Share this page on Facebook',
text: 'Share on Facebook'
});
}
})();
function createEmailShareLink() {
return $("<a />", {
href: mailto:?subject=Check%20this%20out%20&body=url=' + encodeURIComponent(location.href),
title: 'Click to email this to a friend',
text: 'Tell a friend'
});
}
function createLinkedInShareLink() {
return $("<a />", {
href: 'http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(location.href),
title: 'Share this page on LinkedIn',
text: 'Share'
target: '_blank'
});
}
I have jquery included already at the bottom of the page - does this code go in a separate file that's then included after?
<script src="jQuery.js"></script>
<script src="site.js"></script>
</body>
</html>
this would also be great for a simple tell a friend link with mailto - would this work?
Thinking ahead, for the event tracking I can just add the onclick to the outer share-links div
$(this).append(createLinkedInShareLink()) $(this).append(createLinkedInShareLink())
$(that).append(createFacebookShareLink()) How do we get the script to create 1 LinkedIn, 1 Facebook, 1 email a friend, etc.
function appendLinks(el) {
// append links to el
el.appendChild(createLinkedInShareLink());
el.appendChild(createWhiteSpace());
el.appendChild(createFacebookShareLink());
}
Is that what 'this' refers to here
$(this).append(createLinkedInShareLink())
So it would be 'share-link this' for 1 div, 'share-link that' for another, and then
$(this).append(createLinkedInShareLink())
$(that).append(createFacebookShareLink())
Am I on the right track?