0

So I have simple problem. I need to use function when index.php is loaded. For now I can use function by button click, but I need to make that It used automatically when index.php is loaded.

Here is part of index.php

<div id="fb-root"></div>
<a href="#" onclick="FbRequest('This page is amazing, check it out!','4d5da07acbbb0');">Send Request</a> //after user click this, It use function, I need to use this automatically after index.php is loaded

<script type="text/javascript">
function FbRequest(message, data){
        FB.ui({method:'apprequests',message:message,data:data,title:'Share this site with your friends'},
                function(response){
                        // response.request_ids holds an array of user ids that received the request
                }
        );
}
// typical application initialization code for your site
(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());
window.fbAsyncInit = function() {
    FB.init({
        appId   : '0000000000',
        session : {},
        status   : true,
        cookie  : true,
        xfbml   : true
    });
};
</script>

3 Answers 3

0

Add this to your script.

window.onload =FbRequest('This page is amazing, check it out!','4d5da07acbbb0');
2
  • Thank you for answer, but after I use this nothing happens. I've tried to use this in top of my script <script type="text/javascript"> window.onload =FbRequest('This page is amazing, check it out!','4d5da07acbbb0'); function FbRequest(message, data){........ also tried to use this in head and in body but the same. Commented Feb 9, 2014 at 12:06
  • Hmm... I coded the same thing with slight modification in this fiddle jsfiddle.net/BBPLD It seems to work. Commented Feb 9, 2014 at 12:15
0

If you are using jquery in your page then call function after body on load.

inf Jaueyr you can do like

 $(function(){
     //your ajax call to function or  
 });

if you are using core JavaScript hen you can use <body onload="load()">

or

window.onload = function () {
     all your code goes here.
   }
0

If you are using vanilla JavaScript you can bin to onload event.

window.onload = function() {
  FbRequest("Lorem ipsum.");
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.