I want to trigger a Facebook like button as soon as the page loads.
This is the script required to use a Facebook like button:
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
The Facebook like button:
<fb:like href="http://ecommercedeveloper.com" layout="standard" show_faces="true" width="400" action="like" font="segoe ui" colorscheme="light" id="facebook" />
My script, at the bottom of the page:
<script>
(function($){
$('fb:like').trigger('click');
})(jQuery);
</script>
Unfortunately, when the script runs the like button doesn’t seem to be pressed. Is there any way to trigger the like button non-manually?
You can’t. Clicking on the
<fb:like>element has no effect because it’s just a container. The actual clickable “like” button is loaded in an<iframe>inside of it. Because this iframe is loaded fromfacebook.com, the same origin policy prevents you from accessing its contents (including the link you want) unless you are also onfacebook.com.If this security restriction did not exist, you would be able to use the following jQuery:
If this were possible it would represent a massive flaw in the web’s security model. If you find some way to do it, you would find it more profitable to sell the exploit to some criminal organization than to use it generating fraudulent “likes”.