Buy Me A Coffee 😇

Share Article/Link To Social Media Using Pure JavaScript

/ In this post, I will show you that you don't need any library or any packages just to create button share. You can basically create your own share function using pure javascript.

Mar. 17, 2023. 9:28 AM

You don't need to download any packages, instead, you can create your function for sharing a link to social media. In this post, we will focus on the 3 of the most popular social media. Facebook, Twitter, and Facebook.

First, we have to identify or need to know the link to be used for sharing:

  1. Facebook{url}
  2. Twitter{url}
  3. LinkedIn{url}

Now that we identified the share link for this social media, we then create a function something like this:

function share(social) {
    let url = "";
    if (social == "facebook") {
        const navUrl = "" + url;, "_blank");

    if (social == "twitter") {
        const navUrl = "" + url;, "_blank");

    if (social == "linkedin") {
        const navUrl = "" + url;, "_blank");

    if (social == "copy") {
        navigator.clipboard.writeText(url).then(() => {
            alert("Link Copied.");

    alert("social name does not exist.");

The code above accepts a string argument called social. And we can pass facebook, twitter, or linkedin. This is a simple function, and you can modify it any way you like. The return is used to avoid running the code once the social media is found. So it will open a new tab, showing the share page of the social media selected. the url variable is the URL we are going to share. If social media is found it will run the alert() method. (NOTE: the link you are sharing should have the basic SEO meta tags, especially the OG meta tags. Search it in google if you are not sure what are those.)

Then you can create a button for each social media something like this:

<button onclick="share('facebook')">Facebook Share</button>
<button onclick="share('twitter')">Twitter Share</button>
<button onclick="share('linkedin')">LinkedIN Share</button>
<button onclick="share('copy')">Copy Link</button>

Also created a CodePen for this example Check it Here:

If you like you can create your own simple package, you just have to share every social media share link and you can just create those function methods by yourself.

That's it! short read. Hope it helps! Bookmark my website for more post. Later 👋😎👍

If you enjoy this article and would like to show your support, you can easily do so by buying me a coffee. Your contribution is greatly appreciated!

Jenuel Ganawed Buy me Coffee