Solved

Weird Javascript issue in Firefox

Posted on 2011-03-20
6
531 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:manicdiggity
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:GregArnott
ID: 35177569
Tested in Chrome, IE, and Firefox - works fine.

Which version of FF are you using?
0
 
LVL 6

Expert Comment

by:GregArnott
ID: 35177616
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
 
LVL 8

Expert Comment

by:Tocacar
ID: 35179464
@GregArnott:
Won't the browser's javascript interpreter automatically insert the missing semicolon at the end of the onload function?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:manicdiggity
ID: 35183003
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
 
LVL 6

Accepted Solution

by:
GregArnott earned 500 total points
ID: 35185445
@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
 

Author Closing Comment

by:manicdiggity
ID: 35186211
Thanks, works perfect!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now