Monthly Archives: April 2010

JavaScript event-handling

Javascript which existed for some scripting works almost turned into a programming language. There are lots of Javascript libraries to do some animation works such as a floating text animation or some little games. To build these libraries, developers benefit from Javascript’ s object-oriented and event-driven features.

Javascript is an event-driven scripting language which means when a specific action occurs on a web page, you take a reaction- that is, for example a display message. After you make the action there is a cycle until you take the reaction.

Following shows a summary of this process.

  1. When a certain event occurs to act on a specific target object of a web page, it is dispatched and then an event object is created.
  2. This object is passed to functions which are registered to the target object before.
  3. Functions run and a reply is created. They are called Event-Handlers since they handle a specific work when an event occurs. They are also called Event-Listeners since they listen events to happen to handle a specific work. Besides, some authors use “Callback” name.

There are two kinds of events in Javascript.

  • Built-in (i.e. click, hover, load, keypress)
  • User Defined (i.e. myEvent)

To handle these events, we can define event-handlers have names such as “xxxOnload”, “xxxListener”. To register these handlers to target objects javascript uses several approaches. The following shows them.

  1. Inline:

    onclick="function();" is written near tag name.
  2. By using built-in onxxx properties of objects:

    window.onload = function() {....};

    This approach has a drawback. Since onload is a built-in property of window object, only one specific event-handler can be attached to it. If you have an external library , for example a floating text animation, which uses window.onload event-handler to do something when page loads and then one day if you decide to use an other external library which also uses some code to do something when page loads, your library calling code which places after other one will be overridden. Therefore, one of your scripts doesn’t work.
  3. In software engineering perspective:
    window.addEventListener("load",function() {...},false);
    This approach overcomes the drawback i mentioned above, since it doesn’t use built-in onXXX properties. However, this time we have problems about cross browser scripting. When i was talking about event architecture, i skipped some details about event flow for clarity. There are two types of event flow is used by browsers. IE uses event bubbling and Firefox uses event-capturing. The third parameter of addEventListener method says wheter event capture will be used.

    addEventListener method of DOM objects is defined in DOM Level 2. Since IE uses its own implementation this method doesn’t work on IE. IE uses attachEvent() instead of addEventListener(). It also do the same work, but its name is i think insufficient to imply its aim. Its name might be attachEventHandler(). Also, first parameter values take “on” prefix and it doesn’t have third parameter.
    window.attachEvent("onload",function() {...});


Leave a comment

Filed under Web Programming

How to send mail from localhost

Recently, when i was working on one of my hobby projects, i faced some problems about sending mail from localhost.

I tried to send mail using PHP’s mail function. It seems very simple at first look, but when i got following error message,

SMTP Error 550, unknown command.

i got frustrated. After a while I searched this problem and i found some security issues about SMTP protocol. Because of these security issues, sending mail is not easy as it looks.

First, this SMTP error message is related to SMTP server existence. I didn’t have an SMTP server when i got this error message. This means if you are working on a local computer, you must use a mail server. Mercury is a mail server you can use. After installing Mercury, you must configure php.ini. The following part of this file is changed.

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25 -> this is the port that mail server listens.

; For Win32 only.
;sendmail_from =
sendmail_from = postmaster@localhost -> postmaster is admin of mercury mail server.

After configuring the file as stated above, when you try to send your email as xxx@localhost, public SMTP servers(, will probably ignore since it may be spam.

Following article at explains spam issue.
Use TCP Port 587 For Mail Submission
The quote below is from this article:

The conventional way for a mail client program to send e-mail is using TCP port 25, which is also the port used by mail servers to talk to each other. But port 25 is widely abused by malware to spread worms and spam. As a result, many ISPs are restricting its use.

Most users shouldn’t have a problem anyway. If you only send mail using their ISP account, you can probably still send it using the ISP mail server. But if you want to send mail using the mail server for your office, or perhaps a hosted mail server for a domain you own, you could have problems.

After installing mercury, it must be configured to be a Relay SMTP server instead of real SMTP server.
This article at the link below explains why it must be configured as “Relay”.
Apache Friends Support Forum

This quotation is from that article:

The PHP mail()-function supplys only a very simple API in order to send mail via SMTP servers that do *not* require any authorization. Unfortunately, all public SMTP servers (like etc.) require authorization, therefore you *cannot* send mails directly via mail() using these public servers.

If you supply “localhost” as SMTP server, you must (of course) install a local SMTP server, for example Mercury. For the same reason, you must not configure it using any authorization. Mercury comes in several flavours, one of them is a real SMTP server, but usually most mail boxes block mails coming from privat IPs (like yours), these IPs are blacklisted due to SPAM reasons. Therefore it is not usefull to run Mercury as a real SMTP server. Instead, you may install Mercury as a “Relay SMTP” client, which simply relays all mails to a public SMTP server (like

If we want to set Gmail as out Relay SMTP client, we must know Gmail’s E-mail client configuration. It is explained at this link. Then we must configure Mercury SMTP relay client settings according to this configuration.
The article at explains this configuration. I would like to thank for this detailed explanation.

Leave a comment

Filed under Networking

Why to choose Flex

This article below explains why one can use ActionScript to create RIAs.

If you compare the web applications with the desktop ones you can draw these points:

  • Web applications are cool because you can use them wherever you have a browser and an Internet connection. They have lower costs for distribution and maintenance because all you have to do is to update the server with the new version and all the clients are updated.
  • Desktop applications are cool because they offer great UI experience and at the same time are very responsive to the user actions.

Each of these two categories has its own strengths. Wouldn’t be cool to have a third category that combines the reachness and availability of web apps with the richness and responsivness of desktop apps? Actually this category already exists (since 2002 when Macromedia used for the first time this term) and it’s called Rich Internet Applications. With Flex you can build RIAs easily.

Besides, author mentions using PHP with Flex.

Flex is a client side technology; PHP is a server side technology. Both technologies are quite mature: you can find tools, frameworks and libraries, and articles/examples for any task or application type you might think of.

Both technologies are open, and both have the lowest entry point: your time to understand them. Using Flex or Adobe AIR SDK and PHP with vi doesn’t cost a thing, and the same is using the runtimes (Flash Player or Adobe AIR) and using a web server with PHP.

Why choose PHP and Flex

Finally, here is the Author’s blog:

Leave a comment

Filed under ActionScript and Flex

References on Cross-Browser Compatible CSS

When working on moderately large web sites, it seems that the easiest part is writing CSS files straight-forward. However, if you always test your mark-up with your favourite browser you’ll probably have problems when you display that mark-up with other browsers.

You can always find some tips or tricks to solve some particular problems by searching on the web moreover you can spend lots of hours with trial and error.

Recently, i have found some valuable resources on how to write cross-browser compatible css files.

  1. These browser-originated bugs are generally related to how a browser interprets your layout-oriented style properties.
    The first thing we must do to deal with this bugs is understanding css basics right.
    So, we must ask why CSS exists or why it resides “Cascade” word in its abbreviation.
    “Cascade” is related to the “Inheritance” in this context.

    This article below explains the inheritance in CSS with an explanatory example, however, i think that the universal selector-refusing idea can change according to the structure and scope of your web site.

    The second rule explicitly overrides the inheritance of the containing paragraph’s font-size, that the author wished to apply. Since the universal selector will always match a parent’s child elements, it will override any inherited properties.

  2. What if your project gets larger and detailed ?. Since you will probably have many nested elements to be styled, your code must be generic enough. So, how much generic style will be enough?
    This article explains “Generalization in CSS” well.

    So it turns out there’s a spectrum; you can move in either direction, in varying degrees. More dependencies make for a sticky and tangled mess of logic that creates extra work when you come back to modify your creation. More redundancies are verbose and bandwidth intensive, and create extra work for the initial creation process.

    I tend to favour redundancy in theory, but I code in plenty of dependencies in practice. Redundancy is easier to understand, cheap enough when the CSS is being cached properly, but inconvenient to build. Dependencies are far easier to create when coding, but during browser testing and later modifications to the style they cause no end of headaches. And when I’m working on a CSS file that I know will have to be understood by someone else, there’s all the more reason to keep it simple.

Leave a comment

Filed under Favourite Articles

Bayesian Statistics from Endeavour

This article is about distinguishing Frequentist and Bayesian Statistics. It is about a method that we frequently use but don’t define mathematically or formally. It’s a great article. After reading it i believe that life will be more meaningful than it is.

Another great article on the same subject from same blog is,

This article is more obvious than the other. You can easily visualize the example in that article.

Leave a comment

Filed under Favourite Articles, Reference