简体   繁体   中英

Facebook Share - simulate clicking a link from inside SCRIPT tag

I have following simple page:

<body>

<a id='aa' href="https://www.facebook.com/sharer/sharer.php?app_id=370785493758664&sdk=joey&u=https://citizengo.org/&display=popup&ref=plugin&src=share_button" onclick="return !window.open(this.href, 'Facebook', 'width=640,height=580')">SHARE PAGE</a>

</body>
<script>
document.getElementById('aa').click();
</script>

However when loaded, the link is being opened in the same window, but if I comment document.getElementById('aa').click(); and will invoke it from dev tools' console, it shows beautiful popup! WHY?

I want to trigger the popup on page load. I tried other options, but all of them were leading to 'popup being blocked' by the browser and of course I don't want to ask user to add the exception...

I tried other options, but all of them were leading to 'popup being blocked' by the browser

The browser will always block the popup, if you try to trigger it automatically and without proper user interaction.

Try adding target="_blank" to the link, and see if it opens a new window/tab then. (Then you won't be able to specify any dimensions though.)

If that gets caught by the popup blocker as well - then there is no other way.


Slamming this dialog into user's faces without asking, seems a bit aggressive/annoying - sure your visitors will actually like that?

Plus, Facebook might not like it either in the long run, when you force this onto people. They might conclude that you are trying to force users to share, or give the impression that sharing was a necessity to use the rest of your site … So don't be too surprised, should Facebook one day block your domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM