Benny's Blog
Navigation: Home » Programmierung » JavaScript
17. Mai 2012

There is no magic behind checking a web-browsers support for a specific HTML5 feature. If you want to check if a browser supports the latest HTML5 WebSocket API then you just have to take care if there is an object called “WebSocket” available via the DOM:

<script>
  if('WebSocket' in window){
    alert('Hurray! Your browser supports HTML5 WebSockets.')
  }else{
    alert('Sorry, but you are out of date!');
  }
</script>

Nevertheless, there are still people who want to include a fancy library for such an inspection. You can do the same with Modernizr:

<script src="http://www.modernizr.com/downloads/modernizr-latest.js"></script>
<script>
  if(Modernizr.websockets){
    alert('Hurray! Your browser supports HTML5 WebSockets.')
  }else{
    alert('Sorry, but you are out of date!');
  }
</script>
15. Mai 2012

Die folgenden zwei Beispiele zeigen, wie das Tweening und Easing mit KineticJS und mit den CreateJS-Bestandteilen EaselJS und TweenJS funktioniert. Das Beispiel zeigt ein HTML5-Logo in der linken oberen Canvas-Ecke und transformiert dieses Logo beim Anklicken in Richtung Mitte.

…weiterlesen

10. Mai 2012

Die Benutzung von socket.io mit node.js ist einfach aber wird oft unverständlich erklärt. Deshalb schreibe ich kurz das Wichtigste in wenigen Schritten nieder:

  1. node.js v0.6.16 für Windows herunterladen und installieren
  2. C:\Windows\System32\cmd.exe aufrufen
  3. Ins Projektverzeichnis wechseln, z.B.: cd C:\my_project
  4. npm install socket.io eingeben, wodurch socket.io im Ordner “C:\my_project\node_modules\socket.io” installiert wird
  5. Die Datei C:\my_project\server.js mit diesem Inhalt anlegen:
    var io = require('socket.io').listen(72);
     
    io.sockets.on('connection', function (socket) {
      socket.emit('news', {
        hello: 'world'
      });
      socket.on('my other event', function (data) {
        console.log(data);
      });
    });
  6. Die Datei C:\my_project\client.html mit diesem Inhalt anlegen:
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <title>Test</title>
        <style type="text/css">
          * { margin: 0; padding: 0; border: 0; }
        </style>
      </head>
      <body>  
        <script src="http://localhost:72/socket.io/socket.io.js"></script>
        <script>
          var socket = io.connect('http://localhost:72');
          socket.on('news', function (data) {
            console.log(data);
            socket.emit('my other event', { my: 'data' });
          });
        </script>
      </body>
    </html>
  7. Folgende Befehle in der cmd ausführen:
    cd C:\my_project
    node server.js
  8. C:\my_project\client.html im Browser öffnen und das Ergebnis in der JavaScript-Konsole bewundern
9. Mai 2012

If you have installed node.js v0.6.16 for Windows and you want to install socket.io with the following command:

npm install socket.io

Then it could happen that you will get this error message on Windows 7 (64-Bit):

> node install.js
 
npm ERR! Error: spawn ENOTSUP
npm ERR!     at errnoException (child_process.js:483:11)
npm ERR!     at ChildProcess.spawn (child_process.js:446:11)
npm ERR!     at Object.spawn (child_process.js:342:9)
npm ERR!     at spawn (C:\Program Files (x86)\nodejs\node_mod
npm ERR!     at exec (C:\Program Files (x86)\nodejs\node_modu
npm ERR!     at Array.0 (C:\Program Files (x86)\nodejs\node_m
npm ERR!     at EventEmitter._tickCallback (node.js:192:40)

Fortunately, there is a solution! Open this file:

C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\exec.js

Replace these two lines (around line 109 & 110):

  if (!isNaN(uid)) opts.uid = uid
  if (!isNaN(gid)) opts.gid = gid

That’s all. For more information see: Error on socket.io installing on Windows 7.

With these ones:

  if (uid && !isNaN(uid)) opts.uid = +uid
  if (gid && !isNaN(gid)) opts.gid = +gid
28. April 2012

To work with the Facebook API you have to load the Facebook JavaScript SDK, initialize your Facebook application, authenticate the user and request an access token to then send the final Facebook API calls. In this long process chain much can go wrong. Especially because the calls happen all asnychron. You have to work with the correct callback mechanisms to reach the goal. To hide this mechanism and to make the Facebook API easier to use, I have created an abstraction, with which you can work much easier, as you can see here:

<script src="./js/simple-facebook.js"></script>
<script>
  // Setup
  var fb = new SimpleFacebook();
  fb.setAppId('255100001240698');
  fb.setLocale('de_DE');
  fb.setPermissions('email,user_about_me,friends_about_me,publish_actions');
  // Usage
  fb.showUsersName();
  fb.showUsersLink();
</script>

Doesn’t it look great? No more need to include a fb-root tag or many JavaScript snippets. All you need is the simple-facebook.js file, which can be expanded depending on your needs. Here is a basic version, which illustrates the concept:

…weiterlesen

27. April 2012

If you authenticated a user with the Facebook JavaScript SDK, then you can do the following to get the ID, name and email address of a user:

FB.api('/me',function(response){alert(response.id);});
FB.api('/me',function(response){alert(response.name);});
FB.api('/me',function(response){alert(response.email);});