Archive for the ‘JavaScript’ Category.

JavaScript Tween function

This is a small library I wrote for creating simple and complicated easing effects without using JQuery, MooTools or other JS libraries. The library is small 13.4 KB or 5.6 KB compressed, The function can be called as follows: tween(element, property, from, to, duration, [optional] function) Parameters element: The idĀ  of the HTML element you [...]

JavaScript round float to n decimal points

The best you can do with built in JavaScript functions is to round a number to the nearest integer using the method Math.round(x), so to round a float to n decimal points we need to come up with our own function. The function round_float() rounds a floating point number x to n decimals, if you [...]

setAttribute/getAttribute(“style”) crossbrowser alternative

The JavaScript methods setAttribute and getAttribute are very handy when you are designing dynamic effects for your website, but those methods have a lot of bugs in IE (6 an 7 at least), I use these two crossbrowser functions as alternatives instead of using directly setAttribute() and getAttribute(), I’ve been using them for quite a [...]

Print HTML code from JavaScript variable

This also was about to drive me crazy so I will not let it go without writing about it.. Let’s say you have some long HTML code contained on a PHP variable, something like this <?php $some_div = ‘ <div id=”box”> <h1>Title</h1> <p>Article contents + comments</p> <div>’; ?> Now you want to use this HTML [...]

A note on JavaScript arrays creation & initialization

I use PHP a lot in my projects and it happens that sometimes I combine PHP with another language and write completely useless code. PHP arrays can be created and initialized with like this: <?php $cities[] = “mecca”; $cities[] = “medina”; ?> I thought that the same would apply to javascript… Yes, it can work, [...]

Generate passwords with JavaScript

The function generatePassword() is a simple yet customizable function that generates a random password string, the default password length is 6, you can override it by calling the function like thisĀ  generatePassword(n) where n is the password length. The possible characters are defined within the function with the variable chars The function: generatePassword([len=6]) function generatePassword(len){ [...]

Generate a negative random number using JavaScript

Maths Ok, so you already know how to generate positive rondom numbers but let’s do it again: To generate a random number varying from 0 to 9 we will use this code: var randomN = Math.floor(Math.random()*10) This expression on the right of the “=” produces a random number from 0 to 1 (excluding 1, say [...]