Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Share API

JS Web Share API

The Share API can be used to share data to other applications.

What is a Share API?

The Web Share API allows a site to share text, links, files, and other content to user-selected share targets, utilizing the sharing mechanisms of the underlying operating system such as Bluetooth, WIFI, email, WhatsApp, copy link, etc.

The Share API Methods

The share API has just two methods:

Method Description
canShare() Returns a boolean indicating whether the specified data is shareable.
share() Returns a Promise that resolves if the passed data was successfully sent to a share target.
The canShare() Method

The navigator.canShare() method may be used to first validate whether some data is "shareable" or not.

					 
        
          if (navigator.canShare() && navigator.canShare(data)) {
            //  Proceed to share
          } else {
            // Share API not supported
          }
        
      
The share() Method

The navigator.share() method invokes the native sharing mechanism of the underlying operating system and passes the specified data.

It requires transient activation (action taken by user), and hence must be triggered off a UI event like a button click.

					 
        
          <html>
            <body>
              <p id="result"></p>
              <button id="shareButton">Sahre</button>

              <script>
                const shareData = {
                  title: "Lynxsia IT Solutions",
                  text: "Web Share API Example",
                  url: "https://lynxsia.com",
                };

                const btn = document.querySelector("#shareButton");
                const resultPara = document.querySelector("#result");

                // Share must be triggered by "user activation"
                btn.addEventListener("click", async () => {
                  if (navigator.canShare(shareData)) {
                    try {
                      await navigator.share(shareData);
                      resultPara.textContent = "Shared successfully";
                    } catch (err) {
                      resultPara.textContent = `Error: ${err}`;
                    }
                  } else {
                    resultPara.textContent = "Share API not supported";
                  }
                });
              </script>

            </body>
          </html>
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved