HTML consists of statements that define a Web page's elements, controls and appearance. You can place text anywhere inside the body of an HTML document. Unfortunately, browsers remove extra spacing between words in a text string. This increases the time it takes to format some elements such as the HTML marquee. A marquee displays scrolling text that moves across a Web page. To add spaces to text in a marquee, use JavaScript to replace regular spaces with special characters that the browser will not remove.
- HTML consists of statements that define a Web page's elements, controls and appearance.
- To add spaces to text in a marquee, use JavaScript to replace regular spaces with special characters that the browser will not remove.
Open Notepad. Paste this HTML code into an empty document:
This creates a Marquee that contains no text.
Add the following JavaScript function after the "
function set Marquee() {
var original Text = "Marquee String Text";
var marqueeID = "Marquee1";
var blank Character = "&" + "nbsp;"
var new Text = originalText.replace(/ /gi, blank Character);
var marquee Object = document.getElementById(marqueeID);
marqueeObject.innerHTML = new Text;
}
This function sets the marquee text. The variable "originalText" holds the text string that you want to appear in the marquee. In this example, that text is "Marquee String Text." Add as many spaces as you like between the words or characters. "MarqueeID" holds the ID of the marquee. The "replace" method replaces the blanks in the text string with the special HTML character stored in "blankCharacter." The last statement adds the modified text to the marquee object.