Posted
on January 12, 2009, 10:49 am,
by Dan,
under
couchdb.
I always loved and hated databases. When I was a younger programmer, I asked to switch to DB administrator and, thank God, they didn’t agree to let me mutate in a DBA. I ended up fired like 80% of the company (except the DBA). In fact, I wanted to be DBA because I thought I knew how to work with a DB better than them. The rest of my life proved me wrong. Nobody knows what they’re doing because relational databases are a mess.
Then came the non-relational database. Amazon’s SimpleDB was the first NRDBMS (as opposed to RDBMS) that got a public attention. When I read the specs, I really thought it was cool but I wondered “Is it scalable?”. Then I remembered that there is no such thing as a scalable database. Databases need to be smashed and hurt in order to be as scalable as we want them to. I wanted to give it a try but I wasn’t ready to pay for it yet.
Working with xmpp/jabber, I saw a post by the well-known metajack (his blog is mainly about xmpp) about couchdb and I knew the time as come for me to be different. I had to try it so I opened a terminal and wrote sudo apt-get install couchdb. This was the beginning of a great adventure for me…
What’s the link between CouchDB and Javascript? CouchDB is completly JSON. You can write an application that is completly JSON/Ajax without a single line of PHP/Ruby/Python/whatever.
I’m currently writing a simple app for CouchDB and this is the first of some posts about writing an app with CouchDB. I currently use version 0.8.0 and I ran through a lot of problems and I wish these posts will help you succeeding in building an application supported by CouchDB without all the hassle I went through.
Because my main project is TimmyOnTime (and don’t forget to check out our new blog behind the clock), I will work partial-time on this project so it might not go as fast as I (and you) want. But don’t despair, you will still be ahead of the trend when NRBDMS will go mainstream.
Posted
on December 1, 2008, 10:01 am,
by Dan,
under
white belt.
Everytime that I work on a new project, I’m always unsure about which way to execute javascript on page load. These are the different techniques :
Your old buddy body.onload()
The safest way to execute javascript when a page loads is to use the body.onLoad method.
-
<body onload="doSomething();"></body>
The document.ready function with jquery
This technique requires jquery and is similar to the body.onLoad technique (see above).
-
<body><script>$(document).ready()</script></body>
The problem is that I don’t like that the javascript code is too close to the HTML code. There’s really something that I don’t like.
The #ID.ready
DO NOT USE! It doesn’t work. At first, I tought that this technique worked but in fact, even if the element corresponding to the ID doesn’t exists, the function is called. To understand it better, I’ll give you an example. Suppose that I want to load a user list with ajax after the page load and that list correspond to ul#users, I’d write the following code
-
$("ul#users").ready(function() {
-
loadUsersWithAjax();
-
});
Unfortunately, it doesn’t work.
The document.ready + #ID.each
After the failure of the #ID.ready technique (see above), I tried a similar thing that is working but ugly looking. Suppose I want to do the same as above, I’d write this :
-
$(document).ready(function() {
-
$("ul#users").each(function() {
-
loadUsersWithAjax();
-
});
-
});
That way, it would execute the loadUsersWithAjax() method for each ul#users and because element IDs are unique to a page, the method would be called only once. It looks bad but it’s the “prettiest” way I found of executing javascript code on page load.
Now, I want to know what would you suggest? Which technique is your?
Posted
on November 28, 2008, 12:11 pm,
by Dan,
under
white belt.
[Update 2009-09-20 : I will try to bring back ads. I removed them because I was frustrated, I bring them back as an experience.]
JavascriptKata was created in the blog bubble. In a time when all you had to do was to create a blog, put ads on it and you were rich. It never was true. The ads on this blog gave me about 100$ in a year so I finally felt discouraged and left it for dead. Nevertheless, this blog as a good share of traffic and more subscribers in the feed than any blog that I’ve owned/I own/will own. In fact, I call it a blog but it’s not really a blog. I used wordpress and that’s the main reason why I’m calling it a blog.
I tried several things to keep my interest in writing about javascript but they all failed because there was the devil saying “anyways, you don’t make any money out of it”. But there was the angel saying “maybe you don’t make money but you like javascript and should continue to write about it”. As usual, the devil won.
I won’t try to make another revival of this blog but I’ll try to write more about little things that happens between me and javascript, about the daily problems that I encounter. I’ll write a lot about jquery too since for me, there can be no javascript without jquery.
Thanks to all of you!
I receive javascript-related questions in my inbox from time to time and here’s the last one.
I am ok with HTML but awfully new to JavaScript and I know very little at this point. My question is for this web site I’m building. I need to know the exact JavaScript code and placement of the code in an HTML document for this goal. I would like this site to automatically load a certain page (1-31) depending on what day of the month it is (1-31). I don’t need to worry about what month or year it is, just the date in the month. For instance when it is the eleventh day in the month, page11.html will automatically load when you click on to the site. I would like it to take the time(Date) from the client side not the server. This way no mater what time zone your in the right HTML page will pop up at midnight.
Most people can’t give the exact javascript code placement of the code in an HTML document because it would always be buggy. They can give hints and/or snippets of code but rarely a complete working piece. Sorry…
Secondly, relying on javascript to do all this work would be a mistake because if javascript is disabled, the site will not work. Most users have javascript enabled but you should always think about web-crawlers (google, yahoo!, etc) that will try to index your page but will hit an empty page.
It’s always hard to guess all the reasons why someone would want a different page everyday of the month and I don’t know more about the project than what is written above. Considering this, maybe there are just parts of the page that are changing everyday and you could load them on the server-side thus having a single page loading including other pages depending on the day. The problem is that you would have to ask the timezone of the user and keep it in a cookie.
Just remember, javascript should not be a requirement to navigate in a site.
Finally, if I would really want to load a complete new page in javascript, I would do the following.
-
var dayOfTheMonth = (new Date()).getDate();
-
location.href="page" + dayOfTheMonth.toString() + ".html";
This is the way I would do it, do you have any other suggestions?
Posted
on January 30, 2008, 7:57 am,
by Dan,
under
Echo chamber.
I’m not a PHP guy and I like to use standards for the languages I’m using so this php.js thing is not for me at all. In fact, I really dislike PHP and though I use it often, there is no reason in the world I would want to use its syntax to write javascript.
Import the librairie (packed) and write javascript like you never wrote it before.
Example, you want to use the good ol’ strrpos to find position of last occurrence of a char in a string (example from the site)
-
strrpos(‘Kevin van Zonneveld’, ‘e’);
would return 16.
But what happens if a non-PHP guy as to modify your PHPed javascript?
I’m a big fan of jQuery. This librarie is just the best and simplest one around. I really noticed it when I wanted to get rid of jQuery in TimmyOnTime and try to use prototype.js instead, just to be more “rails-oriented” (that’s a pretty lame excuse don’t you think?)
Why I didn’t like prototype.js
There was not a lot of javascript written for the project so I thought that getting rid of jQuery and using prototype.js would be easy. It turned out to be hell on earth. Prototype.js made me feel like I was back in the 90s writing C++. A simple click event turns out to be a awful lot of ugly code with ugly function names.
Example, if I wanted to show the content of a DIV I clicked in prototype.js, I would use things like Event.observe, bindAsEventListener, a mix of native DOM element and prototype.js element, and worst of all, an unintelligible documentation.
jQuery as a complete different way of doing it in a single easy-to-understand line :
$(“#a_div”).click(function() { alert($(this).text()); });.
As simple as that!
The problem
I wanted to use some interface element in Rails that requires prototype.js so I had to have both librairies. The problem is that there’s a conflict between them. Prototype.js doesn’t seem to give a damn about it but jQuery is nicer with you. It offers a noConflict method.
With Rails, prototype.js is the default librairie so to override this, you could do
-
<%= javascript_include_tag "prototype", "jquery" %>
-
<script>$j = jQuery.noConflict();</script>
This way, you will have to use $j instead of $ to call jQuery.$ would still call prototype.
Another thing you could do is to use jQuery on Rails and don’t give a damn about scriptaculous. I personally prefer to use librairies that are fully tested instead of using a “by-pass” that could break my code. It’s your choice…
Posted
on January 21, 2008, 8:18 am,
by Dan,
under
Echo chamber.
As you probably know, I’m a big fan of jQuery because it is more integrated with CSS than scriptaculous. Someone have made a new plotting plugin for jQuery called Flot and this is awesome. The examples are really looking good.
Posted
on January 15, 2008, 11:48 pm,
by Dan,
under
Echo chamber.
If you’re a javascripter, chances are that you coded an infamous javascript calendar in your life. I saw somewhere that at the moment of his death, an normal javascripter will have coded an average of 7.4 javascript calendar. If we estimate that there is 2.3 million javascripters in the world, it means that there will be about 17 020 000 javascript calendar when the first generation of javascripter will be extinct. I don’t remember where I was those numbers but I guess they are accurate…
Today I will present 13.51% of all the calendars that Nathaniel Brown will have done in his life : DateTime Toolbocks. But this one has a little twist. Apart being an ugly looking calendar that shows up when you click on an ugly looking icon, it has an intuitive date input selection. It means that it parses natural language and transform it into a datetime value. You want to select a date to be last week? You simply write last week and the date will be last week.

But there’s a lot more of possible (taken from the site)
- Today
- tod
- tomorrow
- tom
- yesterday
- 6
- 6th
- 6th October
- 3rd of Feb
- 10th Feb 2004
- 14th of Februrary
- 12 feb
- 1 ja
- mon
- Friday
- next Friday
- next fri
- next m
- last Monday
- last mon
- last m
- 2004-8-8 (ISO)
- 2004-04-04
- 1/24/2005 (US)
- 4/26
- 10-24-2005
- Next Week
- Last Year
- Next Month
- 18.11.2004
- 2 years ago
- ten days from now
- 11 years from today
Posted
on January 14, 2008, 7:01 am,
by Dan,
under
Echo chamber.
Gary Haran wrote stuff you should know about bookmarklet and answer the question that’s on everybody’s lips : what is the maximum number of characters that a bookmarklet can have? And the answer is… 508!
I also wrote a complete article on how to write a bookmarklet.
Posted
on January 10, 2008, 6:01 am,
by Dan,
under
Echo chamber.
Closures are really powerful in javascript. The problem with them is that a lot of javascripters don’t understand them well. When I first started working seriously with closures, I had so many questions and no answers… until I read JavaScript Closures for Dummies. Don’t be fooled by the “for dummies” that could make you think about low-quality information as every “for dummies” books. The article of Morris Johns is detailed and precise… but lenghty.
Don’t forget to del.icio.us it and use it as a reference!