Displaying Random Quotation in web page using Java script

Displaying Random Quotation in web page using Java script

We can display a quotation in web page using the following example.

<script language=”JavaScript”>

function showRandomQuote(sec)

{

var Quotation_arr=new Array()

// Define Quotations Array

Quotation_arr[0] = ‘This is first quotation’;

Quotation_arr[1] = ‘This is second quotation’;

Quotation_arr[2] = ‘This is third quotation’;

Quotation_arr[3] = ‘This is fourth quotation’;

Quotation_arr[4] = ‘This is fifth quotation’;

// calculate quot_key by multiplying Math.random value and (quotation array length-1)

var quot_key = Math.round(Math.random()*(Quotation_arr.length - 1));

document.getElementById(’showtextid’).innerHTML = Quotation_arr[quot_key];

setTimeout(showRandomQuote(’+sec+’)', sec*1000);

}

</script>

</head>

<body onload=”showRandomQuote(1)”>

<div id=”showtextid”><!–Quotation will be displayed here–></div>

In above example, we can create a div with id “showtextid”. In showRandomQuote funtion, we can define Quotation array values. “which” variable will give a random key value from Quotation array. This can be calculated by multiplying Math.random() value and Quotation length -1. Finally, we can assign that quotation to “showtextid” div.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.