I have added social sharing buttons for facebook, x, whatsapp and reddit in the body of my site (not the header)on this URL:
I have javascript in the Custom Code > Footer code > Add code before tag.
The intent is that the code populates the social sharing links for each unique page, however it doesn’t fire when I load the page. It does work when I enter inspector and run it myself. Any thoughts on how I could get this code to run on page load so the social sharing links work?
Here’s the javascript code…
window.onload = function() {
console.log(“Social share script is running”); // Debugging log
var currentURL = window.location.href;
var textToShare = encodeURIComponent(“Check out this article!”);
// Facebook
var facebookLink = document.querySelector(‘[data-social-id=“facebook-share”]’);
if (facebookLink) {
facebookLink.href = https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(currentURL)}
;
}
// Twitter
var twitterLink = document.querySelector(‘[data-social-id=“twitter-share”]’);
if (twitterLink) {
twitterLink.href = https://twitter.com/intent/tweet?text=${textToShare}&url=${encodeURIComponent(currentURL)}
;
}
// WhatsApp
var whatsappLink = document.querySelector(‘[data-social-id=“whatsapp-share”]’);
if (whatsappLink) {
whatsappLink.href = https://api.whatsapp.com/send?text=${textToShare}%20${encodeURIComponent(currentURL)}
;
}
// Reddit
var redditLink = document.querySelector(‘[data-social-id=“reddit-share”]’);
if (redditLink) {
redditLink.href = https://www.reddit.com/submit?url=${encodeURIComponent(currentURL)}&title=${textToShare}
;
}
};
Thanks for your help!
Here is my site Read-Only:
https://preview.webflow.com/preview/bip-bap-bop?utm_medium=preview_link&utm_source=designer&utm_content=bip-bap-bop&preview=73f40b04d5ceae76983eb1d56648a638&pageId=671b809ba71bc469e45f3236&workflow=preview