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>
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();
}