Javascript Random Image on page refresh or onload

Some times we need to randomize the images at same place with minimum script and occupy less memory and to decrease the size of a web page. There are lots of demos and examples available on net.I customize the code for one of my projects. It is tested and working fine on all today’s major browsers (IE7, Firefox 3, Safari 3, Google Chrome).

Lets dig into the code…

The Math.floor() method returns the floor of the specified number or expression. The floor of a number is the closest integer that is less than or equal to the number. The Math.random() method returns a random number between 0 and 1. I used a for loop to randomize the max number of images. So that I don’t need to specify each image name in my function. I am passing variable a in img[] array. And variable i will randomize the for loop.

Here is the complete working randomImage() function:

function randomImage(){
var img = new Array();
var img_path = “/path_of/images/”;
var img_name = “image_”;
var img_ext = “.gif”;
var img_id = document.getElementById(”image_id”);
for(var a= 0; a < 8; a++){
img[a] = img_path + img_name + a + img_ext;

}
var i = Math.floor (Math.random() * a);
img_id.src = img[i];

}

Now call the randomImage function in a page where you want to be.
<script type=”text/javascript”>window.onload=randomImage;</script>

You just need to specify the image tag with height and width of the image if all images are of same width and height.
<img id=”image_id” width=”214″ height=”259″ />

And your browser will render the following result:
<img height=”214″ width=”259″ id=”image_id” src=”/path_to/images/image_1.gif”/>

Save your document. And refresh it to get the above result.
If you want to make auto refresh the page use the meta tag:
<meta http-equiv=”refresh” CONTENT=”5;URL=html_document.html”>
Where 5 is the delay of seconds.

Custom Checkbox – The Easiest Way…

I was finding the solution on the net for custom checkbox for one of my projects. Yes there are lots of help and script available, but I found that those are very complicated and with huge scripts. To overcome this I created my simple and easy custom checkbox with crossbrowser compatibility,

What you will need?
1. Two Images: a) Empty Checkbox Image nocheck1, b) Checked Checkbox Image checked
2. CSS
3. Two Javascript functions
… that’s it!

Lets build the custom checkbox.

First create the javascript functions:

function chked(field, image1, image2){
document.getElementById(image1).style.display = ‘none’;
document.getElementById(image2).style.display = ‘block’;
document.getElementById(field).checked = true;
}
function unchk(field, image1, image2){
document.getElementById(image1).style.display = ‘block‘;
document.getElementById(image2).style.display = ‘none’;
document.getElementById(field).checked = false;
}

Now build the CSS:

<div id=”checks“>
<img src=”nocheck.gif” width=”14” height=”14” id=”img1” onclick=”chked(’check1‘, ‘img1‘, ‘img2‘);” />
<img src=”checked.gif” width=”14” height=”14” id=”img2” onclick=”unchk(’check1‘, ‘img1‘, ‘img2‘);” style=”display:none;” />
<input type=”checkbox” name=”check1” id=”check1” class=”noBorder” />
</div>

This will look like below image:
custom_checkbox1

And the result will be:
custom_checkbox2

When the page gets load the unchecked image will be display and onlick event it goes away and checked image will popup instead and the checkbox field will also be checked on same event. So you can pass the value of checked checkbox for your rest of the functionality.

Compare my solution to others:
1. http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/
2. http://slayeroffice.com/code/custom_checkbox/?chkbox1=1&chkbox2=2&chkbox3=3
3. http://www.maratz.com/blog/archives/2006/06/11/fancy-checkboxes-and-radio-buttons/
4. http://www.insidedhtml.com/tips/elements/ts19/page1.asp

Google Chrome Browser – Features

Google Chrome Features:
1. Based on Apple Web Kit and also using Firefox plugins
2. Faster than IE, FF, Safari
3. Takes same time to download the homepage
4. Good support for Javascript
5. Good support for CSS2, CSS3
6. Great support for AJAX
7. Most of the time the entire page get refreshed
8. Legend of Fieldset in Prototype popup comes in a middle like Safari
9. The ‘File’ field displays similar to Safari’s
10. Better for all kinds of Google apps
11. Completely ignores the html comments

… chrome is in a developing process so its possible that some of google’s developer will fix the issues.

Detecting Chrome:
<script type=‘text/javascript’>
window.onload = function test(){
var chrome_name = navigator.appName;
var chrome_ver = navigator.appVersion;
alert(‘Your browser is: ‘+ chrome_name + ‘\nChrome Details:\n’ + chrome_ver);
}

Starting mysql in fedora 8 through terminal

Starting mysql in fedora 8 through terminal
===========================================

MySQL Error:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

Solution:
1. login as root in terminal
[Amol@localhost ~]$ su
Password:

2. Start the mysqld:
[root@localhost Amol]# /sbin/service mysqld start

You will get the following:
Initializing MySQL database: Installing MySQL system tables…

OK
Filling help tables…
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password ‘new-password’
/usr/bin/mysqladmin -u root -h localhost password ‘new-password’
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com [ OK ]
Starting MySQL: [ OK ]

3. Now access the mysql prompt:
[root@localhost Amol]# mysql -u root

You will get the following:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.45 Source distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

4. Create the database:
[root@localhost
project_directory]# rake db:create

5. Migrate the database:
[root@localhost
project_directory]# rake db:migrate

6. Loading the database:
[root@localhost project_directory]# rake db:fixtures:load

7. Now test your url http://localhost:3000 in a browser

Opening an url in same window

Opening an url in same window

First create the javascript function:

function selfWindow(){
window.location.href = ‘http://www.ambedkar.org’;
}

then call that function on ‘onClick’ event on button or on anchor link
<input type=”button” onclick=”selfWindow();” value=”open”>

<a href=”#” onclick=”selfWindow();“>Open this</a>