Solved

Need help getting Javascript to work in Firefox

Posted on 2011-03-15
3
172 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:gurvinder372
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now