[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help getting Javascript to work in Firefox

Posted on 2011-03-15
3
Medium Priority
?
182 Views
Last Modified: 2012-05-11
Hello, I am trying to update a simple home page slider in JS. I kind of have it working, but am having 2 problems:

1) The left arrow doesn't work when you first refresh the page. If you click on the right arrows, then the left ones start working.

2) The right arrows work fine in IE and Chrome, however for some reason you have to click a few times in Firefox to get it working.  How can I fix this?
<!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; 
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()
{
	slider = document.getElementById('testSlider');
	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()
{
	slider = document.getElementById('testSlider');
	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
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 35144805
replace your code by this
<!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
 

Accepted Solution

by:
manicdiggity earned 0 total points
ID: 35158327
Hi, thanks, it mostly works except for Firefox.

In Firefox, I'm still seeing the same problem where you have to click the right arrow a few times before it starts to slide.
0
 

Author Closing Comment

by:manicdiggity
ID: 35221360
The solution still wasn't working 100% in Firefox, but I got the problem solved in another post.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…
Suggested Courses

829 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