parseInt(’08’) returns 0 with Javascript

I came across a rather interesting problem with the Javascript parseInt() function a couple of days ago; if the value is a zero padded string and is ’08’ or ’09’ then parseInt() will return 0. This post looks at why 0 is returned and how to solve the problem.

parseInt(’08’) returns 0

The leading zero in the string tells the Javascript engine that it is an octal number. Because 8 and 9 are not valid numbers in octal, parseInt returns 0. This is expected behaviour because they are not valid octal integers and parseInt returns 0 because the first valid number encountered is a zero.

The solution

After a quick bit of research when I had this problem, I discovered the parseInt function has an optional ‘radix’ parameter which specifies the base to use. By passing 10 as the base it solves the leading 0 issue, for example:

alert( parseInt('08', 10) );

will show the number 8 in an alert dialog, whereas:

alert( parseInt('08') );
 

would display 0.

Conclusion

To be on the safe side it’s probably always a good idea to pass the radix parameter to the parseInt function to ensure the values returned are what you expect them to be, especially if they might contain leading zeroes.

<

Different home page and sub pages with SilverStripe

The SilverStripe CMS makes it very easy to have a slightly different layouts between pages by creating another page type and using $Layout with different layout templates. I did this for the Fabco Ltd website which is a small website with a larger main image on the homepage and a smaller version of this image on the subpages; in all other respects the templates are the same.

Example website

The example website used in this post is Fabco Ltd. I’ve also included two screenshots below showing the homepage and subpage with the different main image.

Homepage:

fabco ltd's homepage

Subpage:

contact page on the fabco website

How to set up slightly different templates with SilverStripe

Create a new file at mysite/code/HomePage.php like so (it doesn’t need anything else in it):

<?php
 
 class HomePage extends Page {
 }
 
 class HomePage_Controller extends Page_Controller {
 }
 

In the main content area of the Page.ss file at /themes/theme-name/templates/Page.ss change $Content/$Form section to include $Layout like so (obviously the id and class of the containing <div> may be different for your template):

<div id="content" class="typography">
 $Layout
 $Content
 $Form
 </div>
 

At /themes/theme-name/templates/Layout create two files: HomePage.ss and Page.ss; these contain the slightly different layouts that will go where $Layout is in the above file.

For the Fabco website HomePage.ss looks like this:

<img id="mainpic" src="/themes/fabco/images/home.jpg" width="770" height="404" alt="fabco ltd" />
 

and Page.ss like this:

<img id="mainpic" src="/themes/fabco/images/subpage.jpg" width="770" height="108" alt="fabco ltd" />
 

Then open the following URL in your web browser to rebuild the database etc, changing “mysite” to the domain name of the site:

http://mysite/dev/build/?flush
 

Now go to the CMS admin and change the page type of the home page to “Home Page” under the “Behaviour” tab as shown in the screenshot below.

modifying the page behaviour in silverstripe

The home page now has a ever so slightly different layout to the other pages. Easy.

Notepad++ fails to update

Notepad++ is a feature rich Windows Notepad replacement. It contains an automatic package update feature in more recent versions but if you are using an older version you will be prompted to download an update package and then apparantly nothing happens.

The prompt

After starting Notepad++, if there is a package update available it will show the following prompt:

an update package is available

This says “An update package is available, do you want to download it?”.

If you click “Yes” then it downloads the update and displays another dialog “Notepad++ is opened. Updater will close it in oreer ot process the installation. Continue?” Clicking “Yes” then appears to do nothing. Starting up Notepad++ again shows the first update prompt shown above again.

Version requirement

The updater only works for Notepad++ versions 5.6.4 and higher. If you have an older version then it will not work, so you have to download an up to date version of Notepad++ from Sourceforge.

Which version

To check which version you are running click the ? menu item in the main menu then “About Notepad++” as shown in the screenshot below.

select about notepad++

This will then show the about dialog with the version number, which I have highlighted with a red box in the final screenshot below.

about notepad++ dialog

After installing and running, Notepad++ will again check for updates and prompt to download and install with the improved updater. This time when prompted to install and restart it actually does.

MySQL table is marked as crashed and should be repaired

How do you fix the error when running a query on a MySQL table and get the following error: "ERROR 145 (HY000) at line 1: Table ‘<tablename>’ is marked as crashed and should be repaired". Very easily, but it may take some time depending how big the table is.

Using PHP’s glob() function to find files in a directory

A couple of weeks ago I posted how to read through a directory with PHP using the opendir() readdir() and closedir() functions and now look at the glob() function. glob() returns the filenames into an array and supports pattern matching so it can be very easy to find e.g. jpg images in a particular directory.

Categories PHP

Amazon’s Kindle DX

I got an email in my inbox yesterday about the new Amazon Kindle DX with Global Wireless which looks pretty cool. It has a 9.7″ e-ink screen (the older Kindle has a 6″ screen) and e-books can be delivered wirelessly in seconds. It’s available for pre-order now for US$489 and will be released on January …

Read more