Archive for the javascript Category

Logo

Lundi dernier (le 21 juillet), nous avons pu assister à 3 présentations dans le cadre des ateliers du lundi. Le premier était une introduction aux attaques de type CSRF par Ori. Clément a ensuite fait une session sur le javascript avancé avec la bibliothèque MooTools. Vous pouvez télécharger les slides (format Open Office) et la mini-application (format zip). Enfin, Ovidiu nous a expliqué ce qu’est BuildBot et comment il fonctionne.

La prochaine édition aura lieu lundi prochain, avec au programme :

  • 10h30 : le protocole HTTP par Ori
  • 11h30 : survol de la technologie des mondes virtuels par Thierry : Qu’est-ce qu’un monde virtuel ? Pour quoi faire? Et dans les années à venir ?
  • 12h30 : UNIX toolbox par votre serviteur (Bruno) : la magie de la ligne de commande, les commandes utiles…

Rendez-vous au 64 (2ème étage à gauche) pour assister aux ateliers.

Click to continue reading

According to the Microsoft documentation, there are two dirty ways to catch such objects DOM events :

Here are two exemples using the "PlayStateChange" event :

HTML:
  1. <!-- integrate the WMP object -->
  2. <object id="Player" type="application/x-ms-wmp" width="300" height="200">
  3. <param name="URL" value="c:\MediaFiles\Seattle.wmv"/>
  4.  
  5. </object><!-- the first one -->
  6. <script for="Player" event="PlayStateChange(newState)">
  7. console.log("newState: " + newState);
  8. </script>
  9.  
  10. <!-- the second one -->
  11. <script>
  12. function OnDSPlayStateChangeEvt(newState) {
  13. console.log("newState: " + newState);
  14. }
  15. </script>

The two script tag properties "for" and "event" don't appear in the HTML standards, nevertheless that way works fine with Internet Explorer 6, 7 and Firefox 2, but doesn't with Firefox 3 .
The second one works on all those browsers, but a problem appears when several player instances are running. How to know which one has fired the event ?

An alternate way is using DOM level 2 events. However, the WMP plugin for Firefox doesn't fire its events in the DOM. Instead of it, the plugin probably look for matching script tags and eval the content it found.

Then, on Internet Explorer, and contrary to all other events in this browser, the ones from WMP objects are not prefixed by "on" (like "onMouseOut", "onClick" ...).
Since the javascript libraries like mootools or prototype follow that Microsoft "standard", their event listeners on DOM elements are useless.

So, the good syntax (which does work only with IE) is :

JAVASCRIPT:
  1. document.getElementById('Player').attachEvent("PlayStateChange",function(newState) {
  2. console.log("newState: " + newState);
  3. }

Click to continue reading

La gestion des dates sous PHP a toujours été compliquée (pour ne pas dire pénible). Aussi, nous avons commencé à réfléchir sur une classe Date (ou DateTime) qui simplifierait cela au cours d'un atelier du lundi.

Nous avons également fait une session intitulée "A la découverte de SproutCore". SproutCore est un framework javascript qui a bénéficié du buzz lord de la dernière Keynote d'Apple. Aussi, il nous semblait intéressant de voir quel est le potentiel de ce framework. Nous sommes arrivés à la conclusion qu'il était encore trop tôt pour juger (pas encore de d'applications en ligne, pas de tutoriels à part le Hello World de base, etc.).

Pour ces 2 sessions, les slides sont disponibles : classe Date pour PHP et A la découverte de SproutCore.

Click to continue reading

logo du RailsCamp

Ruby France, Silicon Sentier et AF83 ont le plaisir de vous annoncer l’organisation du premier RailsCamp (BarCamp/DevCamp dédié à Ruby on Rails) parisien. Il aura lieu le samedi 17 mai 2008 dans le cadre convivial de la Cantine, Paris, France.

Le thème de cette non-conférence est bien sûr Ruby on Rails, mais aussi des sujets afférents : Capistrano, Mongrel, Prototype, Script.aculo.us… L’événement est gratuit et
ouvert à tous, des experts Rails aux débutants.

Il sera suivi le lendemain (le dimanche 18 mai) de 11h à 17h par un MashPit. Cette journée sera dédiée à la réalisation collaborative de mini-projets informatiques basés sur Ruby on Rails.

Pour s'inscrire (c'est gratuit, mais limité en nombre de places) : RailsCamp et MashPit

Click to continue reading

On one of our platforms that uses the geocoding services from the google maps api v2 (using client side javascript) we fell upon a very very weird bug.

For the same city google gives different answers based upon the presence of accents in the query string.

1. When you look for Angoulême (the correct spelling, see the small hat upon the e? called accent circonflexe or by it's html entity name ê) Google's geocoding api gives the incorrect answer of angouleme without the accent.
2. When you look for Angouleme (without the accent) it gives the same incorrect answer angouleme.
3. But when you look for Angoulème (incorrect spelling with an 'accent grave') it outputs the correct answer angoulême !!!

You can test by using the following urls:
angouleme
http://maps.google.fr/maps/geo?hl=fr&oe=utf-8&q=angouleme%2CFR&key=REPLACE BY A VALID GOOGLE API KEY
angoulême
http://maps.google.fr/maps/geo?hl=fr&oe=utf-8&q=angoul%C3%AAme%2CFR&key=REPLACE BY A VALID GOOGLE API KEY
angoulème
http://maps.google.fr/maps/geo?hl=fr&oe=utf-8&q=angoul%C3%A8me%2CFR&key=REPLACE BY A VALID GOOGLE API KEY

The answers are extracted from the json section that looks like this: "SubAdministrativeArea":{"SubAdministrativeAreaName":"Charente","Locality":{"LocalityName":"Angoulême"}}

The same problems appears for other place names with accents but has not been thoroughly tested.

Click to continue reading

Welcome to probably the most useless blog post on the web.

Now you can happily animate stuff in the address bar (do you remember the nineties and nice status bar animations ? :) ) .

The beautiful online demo can be found here : Demo of animated address bar

HTML:
  1. <title>Totally usesless 'ajax' 'loading' image in the address bar as a spinning thing</title>
  2. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  3. <meta name="author" content="AF83.com" />
  4. <script language="JavaScript">

JavaScript:
  1. <!--
  2. /**
  3. *  Copyright (c) 2007, AF83
  4. *  All rights reserved.
  5. *
  6. *  Redistribution and use in source and binary forms, with or without modification,
  7. *  are permitted provided that the following conditions are met:
  8. *
  9. *  1° Redistributions of source code must retain the above copyright notice,
  10. *  this list of conditions and the following disclaimer.
  11. *
  12. *  2° Redistributions in binary form must reproduce the above copyright notice,
  13. *  this list of conditions and the following disclaimer in the documentation
  14. *  and/or other materials provided with the distribution.
  15. *
  16. *  3° Neither the name of AF83 nor the names of its contributors may be used
  17. *  to endorse or promote products derived from this software without specific
  18. *  prior written permission.
  19. *  THIS SOFTWARE IS PROVIDED BY THE COMPANY AF83 AND CONTRIBUTORS "AS IS"
  20. *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  27. *  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *  @copyright  2007 AF83 Ori Pekelman
  32. *  @license    BSD License (3 Clause) http://www.opensource.org/licenses/bsd-license.php
  33. *  @package    Useless Stuff
  34. */
  35. // Configure :
  36. var TextBefore=' ...  Loading ... ';
  37. // End configuration
  38. var AddressBarLoaderTimeOut;
  39. var OriginalLocationHash = document.location.hash; // so we can restore original url
  40.  
  41. function loc_hash(a){
  42.  document.location.hash =TextBefore+a;
  43. }
  44.  
  45. function spinStart(){
  46.     AddressBarLoaderTimeOut=setTimeout("spinStart()",100);
  47.     loc_hash("|");
  48.     loc_hash("/");
  49.     loc_hash("-");
  50.     loc_hash("");
  51.     loc_hash("|");
  52.     loc_hash("/");
  53.     loc_hash("-");
  54.  }
  55.  function spinStop(){
  56.      document.location.hash=OriginalLocationHash;
  57.      clearTimeout(AddressBarLoaderTimeOut) ;
  58.      }
  59. // -->

HTML:
  1. </script>
  2. </head>
  3. <h1>Totally usesless 'ajax' 'loading' image in the address bar as a spinning thing</h1>
  4. <div style="margin:100px;height:100px;width:100px; background-color:#333;font-size:18;color:white;" onclick="spinStart();">Click on me to start address bar ajax loader</div>
  5. <div style="margin:100px;height:100px;width:100px; background-color:#333;font-size:18;color:white;" onclick="spinStop();">Click on me to stop</div>
  6. </body>
  7. </html>

Click to continue reading

Ever needed to select in a form a start and an end time (meeting, event etc..) this little bit of code will help you have a default interval (here 2 hours). When the first combo box changes the second one is set to two hours later.
As usual this javascript is unobtrusive and degradable, just call somewhere initHourCombos() to initialize it. Make sure you change the name $('start') and $('end') if your elements have a different id.

JAVASCRIPT:
  1. /* Make connected combo box 'End' jump 2 hours when combo 'Start' changes
  2. * (c) AF83 2007 Ori Pekelman
  3. * uses code found on http://www.irt.org/script/626.htm
  4. * uses the Prototype library for attaching the event to the combo (you can do without this.. just find the correct element)
  5.  
  6. <label for="start">From :</label>
  7. <select id="start" name="start" class="hour">
  8.   <option value="07:00">07:00</option>
  9.  
  10.   <option value="07:30">07:30</option>
  11.   <option value="08:00">08:00</option>
  12.   <option value="08:30">08:30</option>
  13.   <option value="09:00">09:00</option>
  14.   <option value="09:30">09:30</option>
  15.   <option value="10:00">10:00</option>
  16.  
  17.   <option value="10:30">10:30</option>
  18.   <option value="11:00">11:00</option>
  19.   <option value="11:30">11:30</option>
  20.   <option value="12:00" selected="selected">12:00</option>
  21.   <option value="12:30">12:30</option>
  22.   <option value="13:00">13:00</option>
  23.  
  24.   <option value="13:30">13:30</option>
  25.   <option value="14:00">14:00</option>
  26.   <option value="14:30">14:30</option>
  27.   <option value="15:00">15:00</option>
  28.   <option value="15:30">15:30</option>
  29.   <option value="16:00">16:00</option>
  30.  
  31.   <option value="16:30">16:30</option>
  32.   <option value="17:00">17:00</option>
  33.   <option value="17:30">17:30</option>
  34.   <option value="18:00">18:00</option>
  35.   <option value="18:30">18:30</option>
  36.   <option value="19:00">19:00</option>
  37.  
  38.   <option value="19:30">19:30</option>
  39.   <option value="20:00">20:00</option>
  40.   <option value="20:30">20:30</option>
  41.   <option value="21:00">21:00</option>
  42.   <option value="21:30">21:30</option>
  43.   <option value="22:00">22:00</option>
  44.  
  45.   <option value="22:30">22:30</option>
  46.   <option value="23:00">23:00</option>
  47.   <option value="23:30">23:30</option>
  48.   <option value="24:00">24:00</option>
  49.   <option value="24:30">24:30</option>
  50.   <option value="01:00">01:00</option>
  51.  
  52.   <option value="01:30">01:30</option>
  53.   <option value="02:00">02:00</option>
  54.   <option value="02:30">02:30</option>
  55.   <option value="03:00">03:00</option>
  56.   <option value="03:30">03:30</option>
  57.   <option value="04:00">04:00</option>
  58.  
  59.   <option value="04:30">04:30</option>
  60.   <option value="05:00">05:00</option>
  61.   <option value="05:30">05:30</option>
  62.   <option value="06:00">06:00</option>
  63.   <option value="06:30">06:30</option>
  64. </select>
  65. <label for="end" class="hour">to:</label>
  66. <select id="end" name="end" class="heure">
  67.   <option value="07:00">07:00</option>
  68.   <option value="07:30">07:30</option>
  69.   <option value="08:00">08:00</option>
  70.   <option value="08:30">08:30</option>
  71.  
  72.   <option value="09:00">09:00</option>
  73.   <option value="09:30">09:30</option>
  74.   <option value="10:00">10:00</option>
  75.   <option value="10:30">10:30</option>
  76.   <option value="11:00">11:00</option>
  77.   <option value="11:30">11:30</option>
  78.  
  79.   <option value="12:00">12:00</option>
  80.   <option value="12:30">12:30</option>
  81.   <option value="13:00">13:00</option>
  82.   <option value="13:30">13:30</option>
  83.   <option value="14:00" selected="selected">14:00</option>
  84.   <option value="14:30">14:30</option>
  85.  
  86.   <option value="15:00">15:00</option>
  87.   <option value="15:30">15:30</option>
  88.   <option value="16:00">16:00</option>
  89.   <option value="16:30">16:30</option>
  90.   <option value="17:00">17:00</option>
  91.   <option value="17:30">17:30</option>
  92.  
  93.   <option value="18:00">18:00</option>
  94.   <option value="18:30">18:30</option>
  95.   <option value="19:00">19:00</option>
  96.   <option value="19:30">19:30</option>
  97.   <option value="20:00">20:00</option>
  98.   <option value="20:30">20:30</option>
  99.  
  100.   <option value="21:00">21:00</option>
  101.   <option value="21:30">21:30</option>
  102.   <option value="22:00">22:00</option>
  103.   <option value="22:30">22:30</option>
  104.   <option value="23:00">23:00</option>
  105.   <option value="23:30">23:30</option>
  106.  
  107.   <option value="24:00">24:00</option>
  108.   <option value="24:30">24:30</option>
  109.   <option value="01:00">01:00</option>
  110.   <option value="01:30">01:30</option>
  111.   <option value="02:00">02:00</option>
  112.   <option value="02:30">02:30</option>
  113.  
  114.   <option value="03:00">03:00</option>
  115.   <option value="03:30">03:30</option>
  116.   <option value="04:00">04:00</option>
  117.   <option value="04:30">04:30</option>
  118.   <option value="05:00">05:00</option>
  119.   <option value="05:30">05:30</option>
  120.  
  121.   <option value="06:00">06:00</option>
  122.   <option value="06:30">06:30</option>
  123. </select>
  124.  
  125.  
  126. */
  127. function initHourCombos() {
  128. if ($('start')){
  129. $('start').addEventListener('change',function () {
  130.     $('end').value = addTime (this.value ,"02:00")
  131. },false)}}
  132.  
  133. function addTime(t1,t2){
  134. var m = (t1.substring(0,t1.indexOf(':'))-0) * 60 +
  135.         (t1.substring(t1.indexOf(':')+1,t1.length)-0) +
  136.         (t2.substring(0,t2.indexOf(':'))-0) * 60 +
  137.         (t2.substring(t2.indexOf(':')+1,t2.length)-0);
  138. var h = Math.floor(m / 60);
  139. m=(m - (h * 60));
  140. if (h>24) {h=h-24};
  141. if (m&lt;10) {m='0'+m};
  142. if (h&lt;10) {h='0'+h};
  143.  
  144. t3=(h + ':' + m );
  145.  
  146. return t3;
  147. }

Click to continue reading

You must know the $ Function in prototype (also in jQuery and mootools) that helps you target elements in Javascript. Well, here's my take on it.

Here's the code :

JavaScript:
  1. function $( magic , node ){
  2.       //model : [@][id][:][tag]
  3.  
  4.       var keys = /^([\@]*)([\w+]*)([\:]*)([\w+]*)$/(magic);
  5.       var isArray = keys[1];
  6.       var id = keys[2];
  7.       var tag = keys[4];
  8.      
  9.       if( tag == "" ){tag = "*"}
  10.       if( node == undefined ){ node = document }
  11.      
  12.       var nodes = node.getElementsByTagName(tag);
  13.       var ret = new Array;
  14.       for( var i=0; i &lt;nodes.length; i++ ){
  15.             if( id != "" &amp;&amp; nodes[i].id != id ){continue}
  16.             ret.push( nodes[i] );
  17.       }
  18.      
  19.       if( isArray == "@" ){
  20.             return ret;
  21.       }else{
  22.             return ret[0];
  23.       }
  24.  
  25. }

How to use it :


$("mainHeader")
: returns the first node in document having "mainHeader" for id and anything for tag name.

$("clickMe:a") : returns the first node in document having "clickMe" for id and "a" for tag name.

$("@clickMe:a") : returns all nodes ( as an array ) in document having "clickMe" for id and "a" for tag name.

$("@:div") : returns all divs in document.

$(":img") : returns first img in document

$("@clear:div" , subNode ) : returns all nodes in DOM node "subNode" having "clear" for id and "div" for tag name.

$("@") : returns all DOM nodes in document.

And so on ...

Click to continue reading

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.0 License.