Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Weird Javascript issue in Firefox

Posted on 2011-03-20
6
Medium Priority
?
548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

715 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