Archive for July, 2008

Quick tip

July 22nd, 2008

Hey ya all here i am drinking my beer thinking in boredom about my uber hacktic at the office when i realized, let me quick hint the web developers / designers who are working with css. We all know there is one pain in the ass if you have been doing all your css and FINALY after all of that hard work its time .. Alright lads lets take it to IE6 and see how it works out. Most of the time that is where disaster strikes for me and team. What i always do when divs and such are out of sync on the screen is giving the divs a background color. Last week when this happened again i was happily surprised that that site i was making was using my javascript framework.

For this framework i have been making a small function that marks a div red just plain simple, so let me share this code with you..

JAVASCRIPT:
  1. <script type="text/javascript"><!--
  2.     var oRed = Object.extend  ($('bugged'), {
  3.         makered: function () { this.style.backgroundColor = 'red'; }
  4.     }).makered();
  5. // --></script>

This is very easy isn't it .. Its just basic usage of the prototype framework but for the critics among is yes ithere is a more easy way. But sometimes when you have been in the office all day your eyes whant something as well. Okay here is the easy way :)

JAVASCRIPT:
  1. <script type="text/javasript"><!--
  2.   $('bugged').style.backgroundColor = 'red';
  3. // --></script>

So when ever you have problems with positioning divs (you know position relative / absolute stuff) mark your stuff and you will get to your goal quickly. Alright my beer is almost gone time for an other beer run so im heading off. PS feel free to respond i am all open and all ears.

Dont forget to leave a comment ;)

Announcement to make!

July 21st, 2008

Hello people i have an announcement to make. The next series of posts on this blog we will be creating a javascript framework on one step at at time. We will call this framework "Smallbug" ..It will be a  framework based on the module concept, this allows you to quickly develop the module your project needs.

Smallbug might not the the final name but hey for now lets consider it the codename for this project. Preperations are being taken as you are reading so stay tuned people!.

PHPjs contribution

July 20th, 2008

To spice up my Sunday i have been writing some javascript equivalents to php functions. I have contributed these functions to the PHPjs project.

here is an example for array_walk();

JAVASCRIPT:
  1. <script type="text/javascript"><!--
  2. function callback (value, key, userdata)
  3. {
  4.     console.log (key + ' ' + value + ' userdata ' + userdata)
  5. }
  6.  
  7. function array_walk (array, funcname, userdata) {
  8.     // http://kevin.vanzonneveld.net
  9.     // +   original by: Johnny Mast (http://www.phpvrouwen.nl)
  10.     // *     example 1: array_walk ({'a':'b'} ,'callback', 'userdata');
  11.     // *     returns 1: true
  12.     // *     example 2: array_walk ('a' ,'callback', 'userdata');
  13.     // *     returns 2: false
  14.  
  15.     if (typeof array != 'object')
  16.      return false;
  17.  
  18.     for (var key in array)
  19.     {
  20.       if (typeof (userdata) != 'undefined')
  21.         eval (funcname + '( array [key] , key , userdata  )' );
  22.       else
  23.         eval (funcname + '(  userdata ) ');
  24.     }
  25.     return true;
  26. };
  27. array_walk ({'a':'b', 'c': 'd'} ,'callback', 'test');
  28. // --></script>

here is an example for array_walk_recursive();

JAVASCRIPT:
  1. <script type="text/javascript"><!--
  2. function callback (value, key, userdata)
  3. {
  4.     console.log (key + ' ' + value + ' userdata ' + userdata)
  5. }
  6.  
  7. function array_walk_recursive (array, funcname, userdata) {
  8.     // http://kevin.vanzonneveld.net
  9.     // +   original by: Johnny Mast (http://www.phpvrouwen.nl)
  10.     // *     example 1: array_walk_recursive ({'a':'b', 'c': {'d' : 'e'}} ,'callback', 'userdata');
  11.     // *     returns 1: true
  12.     // *     example 2: array_walk_recursive ('a' ,'callback', 'userdata');
  13.     // *     returns 2: false
  14.  
  15.     if (typeof array != 'object')
  16.      return false;
  17.  
  18.     for (var key in array)
  19.     {
  20.       if (typeof array[key] == 'object')
  21.        return array_walk_recursive (array [key], funcname, userdata);
  22.  
  23.       if (typeof (userdata) != 'undefined')
  24.         eval (funcname + '( array [key] , key , userdata  )' );
  25.       else
  26.         eval (funcname + '(  userdata ) ');
  27.     }
  28.     return true;
  29. };
  30.  
  31. array_walk_recursive ({'a':'b', 'c': {'d' : 'e'}} ,'callback', 'userdata');
  32. // --></script>

Here is my contribution for create_function ();

JAVASCRIPT:
  1. <script type="text/javascript"><!--
  2. function create_function (args, code) {
  3.     // http://kevin.vanzonneveld.net
  4.     // +   original by: Johnny Mast (http://www.phpvrouwen.nl)
  5.     // *     example 1: var myfunction = create_function('a,b', "alert (a + b)");
  6.     // *                myfunction (1,2); /* outputs 3 in an alertbox */
  7.     // *     returns 1: function
  8.  
  9.     eval ('var _oFunctionObject = function (' + args + ') { ' +  code + '}');
  10.     return _oFunctionObject;
  11. };
  12. /* Creates a function and alerts the sum 1+2 */
  13. var myfunction = create_function('a,b', "alert (a + b)");
  14. myfunction (1,2);
  15. // --></script>

Enjoy i hope these functions are useful your any of you ...

Code your Javascript Php style

July 12th, 2008

phpjs

While browsing the web at the office i came across phpJS. PhpJS helps you develop your javascript projects the same way as your php scripts. I am a php dev my self and find this litle framework verry usefull If your at work working with php the whole day you get into some kind of flow (working rithem) winch you can now continue in your javascript code. Ofcource i have made my own javascript equalent framework of the phpframework we are using but by adding this to my framework makes the workflow even better.

Here is the ported function "sort()" made by Kevin van Zonneveld

JAVASCRIPT:
  1. function sort( array, sort_flags ) {
  2. // http://kevin.vanzonneveld.net
  3. // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  4. // *     example 1: sort(['Kevin', 'van', 'Zonneveld']);
  5. // *     returns 1: true
  6.  
  7. var sorter = false;
  8.  
  9. // For now only SORT_NUMERIC has a custom sorter
  10. // and SORT_REGULAR, SORT_STRING, and SORT_LOCALE_STRING
  11. // are all handled with the default sorter
  12. if (sort_flags == 'SORT_NUMERIC') {
  13. sorter = function (a, b) {
  14. return(a - b);
  15. };
  16. }
  17.  
  18. if (sorter !== false) {
  19. array.sort(sorter);
  20. } else {
  21. array.sort();
  22. }
  23.  
  24. return true;
  25. }

And here you have a usage example

JAVASCRIPT:
  1. sort(['Kevin', 'van', 'Zonneveld']);

Phpjs has an own repository winch you can find more information on here
.The repository has anon read access so you can extract the code for your own usage realy easy. If you want to have write access (meaning your willing to contribute to the project) then you have to contact Kevin your self

JAVASCRIPT:
  1. svn co svn://svn.phpjs.org/phpjs/trunk phpjs

So to Kevin and all those others who have been working on this framework i would like to say .. keep on doing it guys !!!.