javascript code doesn't work in firefox

dinkado 0 Tallied Votes 276 Views Share

The code works perfect in IE but I can't get it to work in Firefox. Is there a work around for firefox?
Your time and help are greatly appreciated.
dinkado

<script language="JavaScript">




//put your text here
var theText = "This is test text";

function nextSize(i,incMethod,textLength)
{
if (incMethod == 1) return (72*Math.abs( Math.sin(i/(textLength/3.14))) );
if (incMethod == 2) return (255*Math.abs( Math.cos(i/(textLength/3.14))));
}

function sizeCycle(text,method,dis)
{
	output = "";
	for (i = 0; i < text.length; i++)
	{
		size = parseInt(nextSize(i +dis,method,text.length));
		output += "<font style='font-size: "+ size +"pt'>" +text.substring(i,i+1)+ "</font>";
	}
	theDiv.innerHTML = output;
}

function doWave(n) 
{   
	sizeCycle(theText,1,n);
	if (n > theText.length) {n=0}
	setTimeout("doWave(" + (n+1) + ")", 50);
}
</script>
<div ID="theDiv" align="center">

</div>


<body onload="doWave(0)">

Dani AI

Generated

Brief summary and root cause: the script was relying on browser-dependent behavior (implicit globals and script order) rather than explicitly grabbing and scoping DOM elements and variables. That explains why IE tolerated it but Firefox did not. As observed, the page needed the element reference to be obtained when the element actually exists; and as pointed out, the DOCTYPE/quirks-mode difference can change how browsers accept nonstandard or loose HTML.

Practical, low-risk fixes to make it robust across browsers:

  • Always declare loop and temp variables with let/const or var so they do not become globals.
  • Get the container explicitly at runtime (for example with document.querySelector(...)) rather than relying on implicit identifiers.
  • Replace setTimeout with requestAnimationFrame for smoother animation and to avoid using string-eval calls.
  • Use parseInt(value, 10) to avoid radix surprises.
  • Avoid deprecated <font> tags; use <span> with CSS or inline style instead.
  • Reduce layout thrash by updating the DOM efficiently (reuse nodes, or build a string once per frame and set innerHTML once).

A minimal modern animation loop pattern (illustrative only — adapt your size calculation and DOM update strategy):

const container = document.querySelector('#theDiv');
let frame = 0;
function render() {
  // build/update HTML for this frame (use spans or update existing nodes)
  container.innerHTML = htmlForFrame;
  frame = (frame + 1) % text.length;
  requestAnimationFrame(render);
}
requestAnimationFrame(render);

Troubleshooting checklist: open the browser console for ReferenceError or syntax messages; validate your markup and include a standards DOCTYPE so Firefox runs in standards mode; test after moving the script below the target element or by waiting for DOMContentLoaded. These small changes make the animation work reliably in modern browsers and improve performance and maintainability.

fxm 23 Posting Pro

Works fine here (once the elements are assembled into a page in the proper order).

scrappedcola 11 Posting Whiz in Training

Properly formed HTML is your friend in FF IE won't necessarily care.
Try:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
	<script language="JavaScript">
 //put your text here
var theText = "This is test text";
 
function nextSize(i,incMethod,textLength)
{
if (incMethod == 1) return (72*Math.abs( Math.sin(i/(textLength/3.14))) );
if (incMethod == 2) return (255*Math.abs( Math.cos(i/(textLength/3.14))));
}
 
function sizeCycle(text,method,dis)
{
	var theDiv = document.getElementById("theDiv");
	output = "";
	for (i = 0; i < text.length; i++)
	{
		size = parseInt(nextSize(i +dis,method,text.length));
		output += "<font style='font-size: "+ size +"pt'>" +text.substring(i,i+1)+ "</font>";
	}
	theDiv.innerHTML = output;
}
 
function doWave(n) 
{   
	sizeCycle(theText,1,n);
	if (n > theText.length) {n=0}
	setTimeout("doWave(" + (n+1) + ")", 50);
}
</script>
</head>
 
 
<body onload="doWave(0)">
<div ID="theDiv" align="center">
 
</div>
</body>
</html>

Also note the code I have highlighted. You didn't have a variable assigned to the Div element so it wasn't working for me in FF. Could have been a typo from when you posted but it would cause issues.

dinkado 0 Newbie Poster

Thanks so much scrappedcola!! It works like a charm now.
dinkado

fxm 23 Posting Pro

FWIW: with this DTD

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Firefox will be in 'quirks mode', so this function exactly as originally posted

function sizeCycle(text,method,dis)
{
	output = "";
	for (i = 0; i < text.length; i++)
	{
		size = parseInt(nextSize(i +dis,method,text.length));
		output += "<font style='font-size: "+ size +"pt'>" +text.substring(i,i+1)+ "</font>";
	}
	theDiv.innerHTML = output;
}

works as intended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.