my code stock.com

pascalwacker

Tutorial Part 2.2 - Ready function
by pascalwacker

Init JS + Listen to Event + Ready function

Snippet options

Download: Download snippet as tutorial-part-22-ready-function.php.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

<?php
// We build now an own function that tell's us when the FB.init is done. This function is needed later on!
// Wir fügen nun noch eine eigene Funktion hinzu welche uns sagt wenn der FB.init ausgeführt wurde. Diese Funktion wird später wichtig!

// Place this in the body of your page
// Plaziere diesen Code im body deiner Seite ?>
<div id="fb-root"></div>

<?php
// Init the JS
// Javascript aufrufen
$session = $facebook->getSession();
?>
<script type="text/javascript">
window.fbAsyncInit = function() {
	FB.init({
		appId   : '<?= $facebook->getAppId(); ?>',
		<? if ($session && $session !== null): ?>session : <?= json_encode($session); ?>,<? /* don't refetch the session when PHP already has it*/ endif; ?>
		status  : true, // check login status
		cookie  : true, // enable cookies to allow the server to access the session
		xfbml   : true // parse XFBML
	});
	// Helpervariable to check if FB.init is done
	// Hilfsvariabel um FB.init zu prüfen
	fbApiInitialized = true;
	
	// We gona listen for the login event and relode the page
	// Wir fangen den Login Event ab und laden die Seite neu
    FB.Event.subscribe('auth.login', function() {
      window.location.reload();
    });
};

// And here our FB.init checking function, used similar as document.ready of jQuery
// Und hier die Funktion welche FB.init überwacht, hat einen änlichen effekt wie document.ready bei jQuery
function fbEnsureInit(callback) {
	if (!window.fbApiInitialized) {
		setTimeout(function() { fbEnsureInit(callback); }, 50);
	} else {
		if (callback) { callback(); }
	}
}

(function() {
	var e = document.createElement('script');
	e.src = document.location.protocol + '//connect.facebook.net/<?= $locale; ?>/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
}());
</script>

<?php
// now the JS should be ready! so let's try it, we also need some permissions for example creating events for the user (list of permission is here http://developers.facebook.com/docs/reference/api/permissions/)
// Jetzt sollte Javascript verfügbar sein! Probieren wir es aus, wir benötigen ebenfalls einige Berechtigungen vom User (Liste mit allen Berechtigungn kann hier gefunden werden http://developers.facebook.com/docs/reference/api/permissions/)
?>
<br /><br /><fb:login-button perms="create_event"></fb:login-button><br /><br />

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.