2

I am trying to use the web share API in order to allow users to share the links of article URLs. I want it to be that when a user clicks on the "Share Article" button, the user receives multiple social media and apps that they can share to and when they select the preferred app it sends the URL that I have provided. Im not sure what the issue is, I run the website on GitHub pages but I don't believe that is the issue. Any suggestions or solutions? I will add the code below.

HTML

        <article class="blog__card">
            <div class="blog__image">
                <img src="/assets/img/1in4collegestudents.webp" alt="1 in 4 college students has an STI… We need to do something" class="blog__img">
                <a href="/articles/1-in-4-college-students-has-an-STI…-We-need-to-do-something...html" class="blog__button">
                    <i class='bx bx-right-arrow-alt'></i>
                </a>
            </div>

            <div class="blog__data">
                <h2 class="blog__title">
                    1 in 4 college students has an STI… We need to do something.
                </h2>
                <p class="blog__description">
                    1 in 4 college students has an STI. An extremely alarming statistic that is being ignored. Are you protected? Learn the facts about STIs, prevention tips, and Safe Choice's revolutionary plan.
                </p>

                

                <div class="blog__footer">
                    
                    <div class="blog__reaction-share">
                        <a href="/articles/1-in-4-college-students-has-an-STI…-We-need-to-do-something...html"><button class="aboutus-button"><span>Read Article</span></button></a>
                        <a><button class="aboutus-button" id="share-button2"><span>Share Article</span></button></a>

                    </div>

                </div>
            </div>
        </article>




        <article class="blog__card">
            <div class="blog__image">
                <img src="/articles/img/article banner/Sexually transmitted diseases are highest in the LGBTQ community.webp" alt="Blog article about us 3" class="blog__img">
                <a href="/articles/Sexually transmitted diseases are highest in the LGBTQ community.html" class="blog__button">
                    <i class='bx bx-right-arrow-alt'></i>
                </a>
            </div>

            <div class="blog__data">
                <h2 class="blog__title">
                    Sexually transmitted diseases are highest in the LGBTQ community
                </h2>
                <p class="blog__description">
                    The LGBTQ community is at risk for sexually transmitted diseases. Read about the shocking statistics regarding STD prevalence in the LGBTQ community and how Safe Choice is making sex safer for everyone.
                </p>

                

                <div class="blog__footer">
                    
                    <div class="blog__reaction-share">
                        <a href="/articles/Sexually transmitted diseases are highest in the LGBTQ community.html"><button class="aboutus-button"><span>Read Article</span></button></a>
                        <a><button class="aboutus-button" id="share-button3"><span>Share Article</span></button></a>

                    </div>

                </div>
            </div>
        </article>

Javascript

    // Add event listeners to each share button
    var shareButton2 = document.getElementById('share-button2');
    shareButton2.addEventListener('click', function() {
      // Share the link for the 1 in 4 college students article
      var urlToShare = 'https://safechoiceusa.com/articles/1-in-4-college-students-has-an-STI-We-need-to-do-something...html';
      var title = '1 in 4 college students has an STI… We need to do something.';
      shareLink(urlToShare, title);
    });

    var shareButton3 = document.getElementById('share-button3');
    shareButton3.addEventListener('click', function() {
      // Share the link for the STDs in the LGBTQ community article
      var urlToShare = 'https://safechoiceusa.com/articles/Sexually%20transmitted%20diseases%20are%20highest%20in%20the%20LGBTQ%20community.html';
      var title = 'Sexually transmitted diseases are highest in the LGBTQ community';
      shareLink(urlToShare, title);
    });

    function shareLink(urlToShare, title) {
      // Use the Web Share API (if it's available) to share the link
      if (navigator.share) {
        navigator.share({
          title: title,
          url: urlToShare
        })
          .then(function() {
            console.log('Thanks for sharing!');
          })
          .catch(function(error) {
            console.log('Error sharing:', error);
          });
      } else {
        // If the Web Share API is not available, fall back to a URL-based approach
        var shareUrl = 'https://twitter.com/share?url=' + encodeURIComponent(urlToShare);
        window.open(shareUrl, '_blank');
      }
    }
  • I am not too familiar with GitHub pages, but does it use HTTPS? That's important because the Web Share API requires a secure context to work. Your code itself looks correct to me. – Steffen Mar 09 '23 at 01:24
  • @Steffen github pages does not use HTTPS unless you connect a domain and purchase a certificate. In this case, i have done so, so that shouldn't be an issue – ohmygodimpregnant Mar 09 '23 at 04:52

0 Answers0