Home > JavaScript > JavaScript Message/Text Scroller

JavaScript Message/Text Scroller

November 15th, 2009 admin Leave a comment Go to comments

This is a JavaScript to scroll a list of messages or texts to save spaces on your web page. It will show a text one after another from a list which can be displayed in a single DIV or INPUT element. It is best to implement this one for news texts and texts ads.

Platform

This script can be run well using Mozilla Firefox (any version), Internet Explorer (any version), Opera and Fast Browser.

Preview

SCROLLED MESSAGES
Click the button to play the scroller

Source Code


[html]<html>
<head>
<script language="JavaScript">
//List your messages here
var msg = new Array();
msg[0] = "This is message 1";
msg[1] = "This is message 2";
msg[2] = "This is message 3";

//Here’s the function to scroll your messages
function play(num){
//Check whether it reach your last message
//to go back to the first message
if(num >= msg.length)
num = 0;

//Variable declarations
var tmp_elm = document.getElementById("div_display");
var tmp = tmp_elm.innerHTML;
var tmp_next;

//Scrolling process
var random = Math.floor(Math.random() * msg[num].length);
var tmp_chr = msg[num].charAt(random);
if(tmp == msg[num]){
num++;
setTimeout("play("+num+")",4000);
return;
}
if(tmp_chr == tmp.charAt(random)){
play(num);
return;
}

//Preparing output
var first_part = tmp.substring(0,random);
random++;
var last_part = tmp.substring(random,msg[num].length);
var tmp_next = first_part + tmp_chr + last_part;
tmp_elm.innerHTML = tmp_next;

//Recall play
setTimeout("play("+num+")",200);
}
</script>
</head>
<body onload="play(0);">
<div id="div_display">SCROLLED MESSAGES</div>
</body>
</html>[/html]

Copyright Note

This script is free to use and can be modified as you wish.

Javascript Source
About HTML element
About JavaScript

If you like this code/script, perhaps you would kindly treat me a drink. Any number is welcome. Thanks :)

Related Article

  1. No comments yet.
  1. No trackbacks yet.