Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda