stoimen.com/blog

web developing

When you click over an element

This is easy, on the element can be attached event listener an such event listener is built in as the click event is. It’s simply like that:

<div click="func1()"></div>

This with jQuery

A bit different with jQuery, but with the same effect is:

$('div').click(func1);

When there is a bubbling?

Well, if you just copy paste this code in your page:

$(document).click(function() 
    console.log('clicked on the document');
});
$('div').click(function() 
    console.log('clicked on the div');
});

Note: if you’re using Firefox keep the console.log function, in other case you may replace it with either alert or $.log from the recent jQuery plugin i wrote.

Now if you click over the div element you get both messages, cause there is no bubble canceling. If you’d like to add such canceling, just add this code to the click callback for the div element:

return false;

and now the code should look like:

$(document).click(function() 
    $.log('clicked on the document'); 
});
$('div').click(function() 
    $.log('clicked on the div'); return false;
});

javascript: what is typeof typeof

typeof

If you’ve ever heard about typeof operator in javascript than, how about those few questions:

  1. what is typeof typeof ‘string’ ?
  2. what is typeof typeof?

Answers

The first one is “string”. Yes if there you have the following code:

typeof something_undefined

that gives of course undefined, but if there is typeof typeof something_undefined the answer is string, because “udefined” itself is a string.

The second question is simple javascript error, there is no case with typeof typeof only.

jQuery debug plugin

Console.log()

Almost everybody using Firebug extension of Firefox is familiar with console object and in common with console.log method, who’s taking an object parameter and dumps it into the Firebug console. It’s perfect for debugging and it’s really useful.The problem is that if you don’t have Firebug installed or it’s disabled the console object is undefined. The same may occur if you’re using other browser. That’s why I decided to make a simple plugin for jQuery with the $.log interface which checks for the console and opera objects and if they don’t exists just alert the message. It’s nothing special.

Read the rest of this entry »

starting cronjob on Mac

It’s as easy as starting a cronjob on Linux

Yeah it shouldn’t be different. You simply execute in the terminal the well known command

crontab -e

to start editing your crontab file. The crontab (cron table) in breve is the place where you put the scripts you want to automate. Its exactly the same syntax as on Linux.

Than you restart the cron process

The most simple way is to force quit it from the activity monitor, the process’s called cron as you can see on the following image.

How to detect the browser speed?

It’s easy, you just put a ajax or image load in the beginning of the page and than you know what the speed is. But the problem is that the ajax and the image load need to finish to know exactly how slow or fast the connections is. And even worse, you need to slow down the page even for normal (fast) connections. And where’s the point? You get the site slower only to understand the connection. That’s not OK.

The javascript blocks the page load

That’s another problem with javascript. You know when you have script tag in the head of the document, the browser needs to finish loading this javascript, parse it and than continue loading.

How javascript can be loaded async

Well there are several methods, but here we need only one separate javascript that don’t communicate with other JS code in the site, it only get information about the connection speed.

Put the javascript code in an Iframe.

Than the loading of that javascript is async. Independently you continue loading the site. For fast connection, yes this going to slow down a little bit, but however not as much as if you put an inline code. Than in that iframe you can simply check for an expression to verify for a slow connection if it’s true you may set a cookie for slow connection site version and reload the window.parent of the iframe, which is the page itself.