Weird Javascript issue in Firefox

I am having this weird issue with this Javascript in Firefox. The back arrows work fine, but you have to click the forward errors several times to get it going, but it eventually works. Any ideas on how to fix this? You can see an example of the code attached at:

http://64.78.25.66/hometicker.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
        <title></title>
        <script language="javascript">// JavaScript Document
                var bodyIntervalId = 0;
                var sliderIntervalId = 0;

                var slider;
                window.onload = function(){
                        slider = document.getElementById('testSlider');
                }
                var leftpix = 0;
                var maxwidth = 408;
                var sliding = false;
                var amount = 204;
                var count = 0;

                function initialize()
                {
                        var bodyIntervalId = setInterval('Slide(1)', 3000);
                }

                function Slide(inc)
                {
                        if (sliding) { return; }
                                
                        if (inc == "") { inc = 1; }
                        
                        count++;
                        // give up after a while
                        if (count > 20) clearInterval(bodyIntervalId);
                        sliding = true;
                        leftpix -= inc * amount;
                        
                        var limit = (maxwidth - amount) * -1;
                        if (leftpix < limit) 
                        { 
                                leftpix = 0; 
                                slider.style.left = leftpix + 'px';
                                sliding = false;
                                return;
                        }
                        if (leftpix > 0) 
                        { 
                                leftpix = limit; 
                                slider.style.left = leftpix + 'px';
                                sliding = false;
                                return;
                        }
                        
                        if (inc > 0)
                        {
                                sliderIntervalId = setInterval('MoveRight()', 10);
                        }
                        else
                        {    
                                sliderIntervalId = setInterval('MoveLeft()', 10);
                        }
                }

                function MoveRight()
                {
                        
                        var left = slider.style.left.replace('px','');
                        if (left > leftpix)
                        {
                                left = left - 17;
                                slider.style.left = left + 'px';
                        }
                        else
                        {
                                sliding = false;
                                clearInterval(sliderIntervalId);
                        }
                }

                function MoveLeft()
                {
                        var left = slider.style.left.replace('px','');
                        if (left < leftpix)
                        {
                                left = (left * 1) + 17;
                                slider.style.left = left + 'px';
                        }
                        else
                        {
                                sliding = false;
                                clearInterval(sliderIntervalId);
                        }
                }
        </script>
</head>
<body>    
        <div style="float:left; width:20px; height:133px; padding-top:20px; text-align:left;"><a href="javascript:Slide(-1);"><<</a></div>
        
        <div style="float:left; width:204px; height:200px; overflow:hidden; position:relative;">
            <div id="testSlider" style="width:408px; position:absolute; top:0; left:0;">
                                <div style="float:left; width:204px; text-align:center; vertical-align:middle;">
                        <a id="test1" href="/">TEST 1</a><br /><br />
                </div>
                                <div style="float:left; width:204px; text-align:center; vertical-align:middle;">
                        <a id="test2" href="/">TEST 2</a><br /><br />
                </div>
            </div>
        </div>        
        <div style="float:left; width:20px; height:133px; padding-top:20px; text-align:right;"><a href="javascript:Slide(1);">>></a></a></div>
   
</body>
</html>

Open in new window

manicdiggityAsked:
Who is Participating?
 
GregArnottConnect With a Mentor Commented:
@Tocacar - Firebug was falling over from the missing ';' preventing any debugging as I stepped through the code. I'd just presumed FF was behaving badly from the same point.

The main reason the code isn't working - FF doesn't like ' or " in setInterval().

I gave the code a little shake up using jslint as a guide, and probably went overboard with data checking such as parseInt on left values, and lowercase alteration of function names.

Reordering the functions does help with functions being called AFTER they're defined.

Here's the code that works in FF4 and IE8. To drop it back into your page, you'll need to change the onclicks to slide or the function slide to Slide() as well as all other references.
	var bodyIntervalId = 0;
	var sliderIntervalId = 0;
	var slider;
	window.onload = function(){
		slider = document.getElementById('testSlider');
	};
	var leftpix = 0;
	var maxwidth = 408;
	var sliding = false;
	var amount = 204;
	var count = 0;

	function moveright() {
		var left = parseInt(slider.style.left.replace('px',''));
		if (left > leftpix) {
			left = (left * 1) - 17;
			slider.style.left = left + 'px';
		} else {
			sliding = false;
			clearInterval(sliderIntervalId);
		}
	}

	function moveleft() {
		var left = parseInt(slider.style.left.replace('px',''));
		if (left < leftpix) {
			left = (left * 1) + 17;
			slider.style.left = left + 'px';
		} else {
			sliding = false;
			clearInterval(sliderIntervalId);
		}
	}

	function slide(inc) {
		if (sliding) { 
			return; 
		}
		if (inc === '') { 
			inc = 1; 
		}
		count++;

		// give up after a while
		if (count > 20) {
			 clearInterval(bodyIntervalId);
		}

		sliding = true;
		leftpix -= inc * amount;
		var limit = (maxwidth - amount) * -1;
		if (leftpix < limit) {
			leftpix = 0;
			slider.style.left = leftpix + 'px';
			sliding = false;
			return;
		}
		if (leftpix > 0) {
			leftpix = limit;
			slider.style.left = leftpix + 'px';
			sliding = false;
			return;
		}
		if (inc > 0){
			sliderIntervalId = setInterval(moveright, 10);
		} else {
			sliderIntervalId = setInterval(moveleft, 10);
		}
	}
	
	function initialize() {
		var bodyIntervalId = setInterval(slide, 3000);
	}

Open in new window

0
 
GregArnottCommented:
Tested in Chrome, IE, and Firefox - works fine.

Which version of FF are you using?
0
 
GregArnottCommented:
Sorry... misread your post and checked out back instead of forward.

First, fix up script tag. FF doesn't like it.
"If <script> tags have a "type" attribute it should equal "text/javascript" or "application/javascript""

Next, this function has no ; after it:
window.onload = function(){
slider = document.getElementById('testSlider');
}  <-- needs ';' here

Open in new window


Problem at line 14 character 22: Implied eval is evil. Pass a function instead of a string.
  var bodyIntervalId = setInterval('Slide(1)', 3000);
Problem at line 42 character 20: Implied eval is evil. Pass a function instead of a string.
  sliderIntervalId = setInterval('MoveRight()', 10);
Problem at line 46 character 20: Implied eval is evil. Pass a function instead of a string.
  sliderIntervalId = setInterval('MoveLeft()', 10);

At the moment, JS on FF is following this path first load:
if (leftpix < limit)
{
leftpix = 0;
slider.style.left = leftpix + 'px';
sliding = false;
return;
} 

Open in new window


The ';' missing from your onload may be causing FF to ignore your leftpix variable definition, this causing this first condition to be true.

Pass js through jslint after done and these problems should clear up.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
TocacarCommented:
@GregArnott:
Won't the browser's javascript interpreter automatically insert the missing semicolon at the end of the onload function?
0
 
manicdiggityAuthor Commented:
Hi Greg, thanks for the comment, however those changes didn't fix the problem:

http://64.78.25.66/hometicker.html

I still have to click the right arrow a few times before it gets going.  Do you see anything else that might be wrong? I have attached the updated code, thanks.

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

<html xmlns="http://www.w3.org/1999/xhtml">
        <title></title>
        <script type="text/javascript">// JavaScript Document
                var bodyIntervalId = 0;
                var sliderIntervalId = 0;

                var slider;
                window.onload = function(){
                        slider = document.getElementById('testSlider');
                };
                var leftpix = 0;
                var maxwidth = 408;
                var sliding = false;
                var amount = 204;
                var count = 0;

                function initialize()
                {
                        var bodyIntervalId = setInterval('Slide(1)', 3000);
                }

                function Slide(inc)
                {
                        if (sliding) { return; }
                                
                        if (inc == "") { inc = 1; }
                        
                        count++;
                        // give up after a while
                        if (count > 20) clearInterval(bodyIntervalId);
                        sliding = true;
                        leftpix -= inc * amount;
                        
                        var limit = (maxwidth - amount) * -1;
                        if (leftpix < limit) 
                        { 
                                leftpix = 0; 
                                slider.style.left = leftpix + 'px';
                                sliding = false;
                                return;
                        }
                        if (leftpix > 0) 
                        { 
                                leftpix = limit; 
                                slider.style.left = leftpix + 'px';
                                sliding = false;
                                return;
                        }
                        
                        if (inc > 0)
                        {
                                sliderIntervalId = setInterval('MoveRight()', 10);
                        }
                        else
                        {    
                                sliderIntervalId = setInterval('MoveLeft()', 10);
                        }
                }

                function MoveRight()
                {
                        
                        var left = slider.style.left.replace('px','');
                        if (left > leftpix)
                        {
                                left = left - 17;
                                slider.style.left = left + 'px';
                        }
                        else
                        {
                                sliding = false;
                                clearInterval(sliderIntervalId);
                        }
                }

                function MoveLeft()
                {
                        var left = slider.style.left.replace('px','');
                        if (left < leftpix)
                        {
                                left = (left * 1) + 17;
                                slider.style.left = left + 'px';
                        }
                        else
                        {
                                sliding = false;
                                clearInterval(sliderIntervalId);
                        }
                }
        </script>
</head>
<body>    
        <div style="float:left; width:20px; height:133px; padding-top:20px; text-align:left;"><a href="javascript:Slide(-1);"><<</a></div>
        
        <div style="float:left; width:204px; height:200px; overflow:hidden; position:relative;">
            <div id="testSlider" style="width:408px; position:absolute; top:0; left:0;">
                                <div style="float:left; width:204px; text-align:center; vertical-align:middle;">
                        <a id="test1" href="/">TEST 1</a><br /><br />
                </div>
                                <div style="float:left; width:204px; text-align:center; vertical-align:middle;">
                        <a id="test2" href="/">TEST 2</a><br /><br />
                </div>
            </div>
        </div>        
        <div style="float:left; width:20px; height:133px; padding-top:20px; text-align:right;"><a href="javascript:Slide(1);">>></a></a></div>
   
</body>
</html>

Open in new window

0
 
manicdiggityAuthor Commented:
Thanks, works perfect!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.