Solved

Weird Javascript issue in Firefox

Posted on 2011-03-20
6
539 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS does not refresh 6 30
Jquery 2 23
JS Event Does not Trigger From File 2 28
Understanding UserAgent String 2 16
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

830 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