Category: Javascript

Example scripts and howtos for Javascript

Javascript is a programming or scripting language primarily used for client-sided programming in web browsers. It can also be used as a server-side programming language but this is generally used less often. Javascript was originally created by Netscape for their browser Netscape Navigator, and is now an implementation of the ECMAScript standard. It is typically used in web browsers for form validation, floating navigation, AJAX and other nifty things to make web pages more interactive.

Fixed number of digits after the decimal place with Javascript

As well as supporting various rounding functions, Javascript has the toFixed() method to have a specific number of digits after the decimal place. This function works in all modern browsers including Internet Explorer from 5.5+.

Number.toFixed(digits)

Simply pass the number of digits that should be after the decimal place and the string returned will be rounded to that number if there are more or zeros added if there are less.

Some examples:

document.write( 1.1.toFixed(2) + '<br>' );
 document.write( 1.12.toFixed(2) + '<br>' );
 document.write( 1.123.toFixed(2) + '<br>' );
 document.write( 1.125.toFixed(2) + '<br>' );
 document.write( 1.125.toFixed(4) + '<br>' );

These will write out to the document:

1.10
 1.12
 1.12
 1.13
 1.1250

See also my rounding numbers with Javascript post for some other rounding methods which do not set a fixed number of digits after the decimal place.

Read More »

Javascript frame buster

It’s really annoying when instead of linking directly to your site in a full browser window another site links using an <iframe> or <frameset> <frame>. There’s a really simple method to bust out of frames using Javascript but as I discovered yesterday if you also use iframes in your own site the commonly used frame buster needs some improvement.

Read More »

Javascript UNIX timestamp converter

I often need to convert UNIX timestamps from log files into a human readable format and need a quick and easy to access tool for doing so, and have written up this post with the converter tool followed by an explanation of the Javascript code behind it.

Read More »

Toggling a password field between plain text and password

Some time back I posted how to show plain text in a password field and make it a regular password field on focus using jQuery, so that it could show some default text (i.e. "password") before anything was entered and then work like a normal password field when the user started typing text into it.

As someone a couple of days ago pointed out in the comments on that page, while it’s not possible to change the type of a password input with jQuery, it is with regular Javascript. So this post shows how to do just that and which browsers do and don’t support it.

Read More »

Style HTML Anchor Titles with jQuery and CSS

The title property of anchor tags in HTML show the text in black on a yellow background by default in most browsers, after mousing over the anchor for a second or two. This tutorial will show you how to style the title with CSS using jQuery and a small amount of Javascript and it will appear as soon as the user mouses over the link.

Read More »

Get the current page path and filename with Javascript

A couple of days ago, I needed to get the current page’s path and script filename with Javascript, excluding the protocol prefix, domain name and any parameters or hashtags which might be present. So here’s another page for future self reference and so I will remember it in the future.

Read More »