# Issues Triggering Javascript Function

**URL:** <https://answers.netlify.com/t/issues-triggering-javascript-function/137421>\
**Category:** Opentalk\
**Tags:** ux\
**Created:** [January 17, 2025, 12:36pm UTC](https://answers.netlify.com/t/issues-triggering-javascript-function/137421 "2025-01-17T12:36:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattbipbapbop](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/mattbipbapbop/32/51997_2.png) [@mattbipbapbop](https://answers.netlify.com/u/mattbipbapbop)\
**Post date:** [January 17, 2025, 12:36pm UTC](https://answers.netlify.com/t/issues-triggering-javascript-function/137421/1 "2025-01-17T12:36:30Z")

</div>

Hi Folks,

This is my first time using javascript functions and I’m having a problem triggering the function.

I’m looking to add social sharing options to lots of pages on my website and want to use a javascript function to add the page information into the URL of the link. The code works if I go into inspector and trigger it manually, however doesn’t work automatically on page load.

This is all implemented on: [10 Bat Coloring Pages (Free Printable PDFs)](https://www.bipbapbop.com/bat-coloring-pages)  
And my netlify site name is: `lovely-semifreddo-4bea40`

I’d be very grateful for any help!

Here’s an example of the social media link:

![share on facebook]()

Here’s an example of the javascript:

window.onload = function() {  
console.log(“Social share script is running”); // Debugging log

try {  
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)}`;
  console.log("Facebook link updated");
} else {
  console.warn("Facebook link not found");
}

```

} catch (error) {  
console.error(“An error occurred in the social share script:”, error);  
}  
};

---

<div class="post-metadata">

**Author:** ![kylesloper](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/kylesloper/32/18398_2.png) [@kylesloper](https://answers.netlify.com/u/kylesloper)\
**Post date:** [January 17, 2025, 9:26pm UTC](https://answers.netlify.com/t/issues-triggering-javascript-function/137421/2 "2025-01-17T21:26:16Z")

</div>

Hi @mattbipbapbop, I’m going to move this to the `open talk` category as this is something outside of the scope of support we can provide. (We handle all things Netlify related here 🙂 )

Though here is a pointer to get you going,

I’d definitely recommend using an event listener over window.onload… much more reliable for all use cases.

```auto
document.addEventListener('DOMContentLoaded', initializeSocialSharing);
window.addEventListener('load', initializeSocialSharing);

```

All the best,
