Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Auto trigger click button Javascript

How to auto click the "share-button" after page loading page. I have tried number 1 method but still cannot auto click. Number 2 is the original code. Currently still require me to manually click the share-button.

1.

This one using "window.onload" method.

 <div>
            
            <button id="share-button" onclick="autoClick()">Button</button>
        </div>
    </div>

function autoClick() { FB.ui({ display: 'popup', method: 'feed', link: 'https://developers.facebook.com/docs/',

        }, function (response) {
            //Debug response (optional)
            console.log(response);

        });
    }
    window.onload = function () {
        document.getElementById('share-button').click();
    }
<div id="buttons">
   
    <div>
        <button type="button" id="share-button">Continue</button>
    </div>

</div>

<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'ID',
            autoLogAppEvents: true,
            xfbml: true,
            version: 'v11.0'
        });
    };

    

    document.getElementById('share-button').addEventListener('click', function () {
        FB.ui({
            display: 'popup',
            method: 'share',
            href: 'https://developers.facebook.com/docs/',

        }, function (response) {
            //Debug response (optional)
            console.log(response);
        });
    });

    //Load the JavaScript SDK asynchronously
    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementsById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "https://connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));


</script>

<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The approach is just wrong. Instead of "clicking" a button, you should write the listener in a separate function, and call that function instead.

Something like:

document.getElementById('share-button').addEventListener('click', share);

function share() {
        FB.ui({
            display: 'popup',
            method: 'share',
            href: 'https://developers.facebook.com/docs/',

        }, function (response) {
            //Debug response (optional)
            console.log(response);
        });
}

So that you can do at any time, something like:

    window.onload = function () {
        share();
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!