Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda