Wednesday, May 19, 2010

Base Dependency, JavaScript and how parseInt() returns 0

I learned today that the parseInt() function in JavaScript actually has a second parameter that is optional. This second parameter defines the number's base. Thus, you can parse an integer that is binary, octal and hexadecimal. However, if you do not define the base, the parseInt function takes a guess at the base instead of defaulting to decimal.

So, if you have a parseInt('08') or parseInt('09'), the function will return 0. This is because it is guessing that these are octal numbers (based on the 0 in front) and then returns a 0 because they are invalid octal numbers.

The fix is simple, define the base. Use parseInt('08',10) or parseInt('09',10) instead. This blog post helped me and has more info on the matter.

Thursday, May 13, 2010

Hash Changing, detecting the Back button in JavaScript

If you use gmail and you are a web developer, you have probably noticed that they use hash or anchor links (e.g., "example.html#hash") to make your browser's back button very effective. These hash or anchor links were designed to bring your browser to a certain position in a page. Often they are used on help pages or FAQ pages to make the browser window scroll to a certain point. But, interestingly, they can also be used to set certain JavaScript states, when configured properly.

In order to monitor the hash state and determine if the browser's back or forward button were hence used, you must set up a polling loop. It will check the current hash against the last hash. If there is a change it will do something magical (whatever you want to do).
<script type="text/javascript">
/* set defaults */
var curHash = window.location.hash;

/* check for a change in hash (back or forward buttons) */
setInterval(function() {
 if (curHash != window.location.hash) {
  curHash = window.location.hash;
  /* -- do what you need to do when the hash changes! -- */
}, 250);

The check time is set to 250 ms. But this can be tweaked for your particular needs.

There is rumors that in HTML 5 there will be a hash onchange event. This will really clean up the code, removing the need for polling. Until then, this is the next best thing.

Update 10/28/13 - Side Note: Checkout HTML5's push state for doing cool things like this w/o the hash. You can have the whole URL reflect a change in the DOM without the browser trying to go out and fetch a new web page. Pretty cool stuff. Some JavaScript frameworks like Backbone support both the hash change and the newer push state methods.

Monday, May 3, 2010

PEAR Mail.php - Failed to add recipient (code: 451)

I decided to use the PEAR mailer for a web project in order to send emails through my SMTP server. However, I was getting a strange error:
Failed to add recipient: [SMTP: Invalid response code received from server (code: 451, response: Temporary local problem - please try later)]

Yikes! That is not a very helpful error. If the server doesn't know what what the response code means, how am I supposed to? Using SSH, I took a look at the exim_rejectlog located at /var/log and found that my mail server didn't particularly care for the source or 'From' email address.

After a bit of trial and error, it became clear that 'From' address must match the SMTP account's address. It is a nice anti-abuse feature (for shared servers) that I wasn't even aware of.

Anyway, if you get this error, it probably could be a number of things, but be sure to check that your 'From' email address matches the email address of the SMTP account you are using to send the email.