Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-19-2013, 01:19 AM   PM User | #1
cozart
New Coder

 
Join Date: Aug 2010
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
cozart is an unknown quantity at this point
need help calling external script

so i'm very green when it comes to JavaScript and i realize this is probably an extremely basic question, but i'm at a loss and looking to learn.

i have a content slider script that should rotate content slides at the given interval and also apply the '.current' class to the corresponding thumbnail when that particular slide is active.

i'm just unsure how to call the script in my html. below i'll post, in order, what's in my html header (the script is called sliderscript.js), my first two html entries (there's five slides total), and my script.

Code:
<script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="scripts/sliderscript.js" type="text/javascript"></script>
Code:
<div class="slider-container">	
			<div class="slides">	
			
				<div class="slide dummy1">
					<div class="inside">
						<div class="text">
							<p>lorem ipsum</p>
						</div><!--text-->
					</div><!--inside-->
				</div><!--slide dummy1-->
		
				<div class="slide dummy2" style="display: none">
					<div class="inside">
						<div class="text">
							<h1 class="heading">lorem ipsum</h1>
							<p>lorem ipsum</p>
						</div><!--text-->
					</div><!--inside-->
				</div><!--slide dummy2-->
                         </div><!--slides-->

             <div class="slidertray">
		<div class="thumbs-container">
				
			  <ul>
				<li id="dummy1-thumb" class="current first">
					<a href="#">
						<div class="thumbbox"><img src="path" ></div>
						Dummy1 Tag
					</a>
				</li>

				<li id="dummy2-thumb">
					<a href="#">
						<div class="thumbbox"><img src="path"  /></div>
						Dummy2 Tag
					</a>
				</li>
                         </ul>

                   </div><!--thumbs-container-->
               
                 </div><!--slidertray-->

</div><!--slider-container-->
Code:
$(function() {

	/* SLIDER */
	var carousel = (function () {
		
		var	init = function () {
			clickThumb($(thumbs + ' a'));
			autoCycleOn = true;
			pauseAutoCycle('.slides'); // Pause when hoversing over a slide
			pauseAutoCycle(thumbs + ' a'); // Pause when hovering over a link
			killAutoCycle('.video-box a'); // Stop when you play a video
			$(document).ready(function(){ autoCycle; });
			slidePointer($(thumbs + '.current'));
		},
			
		thumbs = '.slidertray li',
		thumbsList = '.slidertray ul',
		noThumbs = $(thumbs).length,
		killedAutoCycle = false,
		cycleSpeed = 5000,
		fadeSpeed = 750,
		autoCycleOn,
		
		// Show slide
		showSlide = function(currentThumb){ 
			var id = currentThumb.attr('id').slice(0,-6); // remove "-thumb" from the id to get the name
			var content = '.slide.'+id; 
			if($(content).length !== 0){ 
				$('.slide').fadeOut();
				$(thumbs).removeClass('current');
				$(content).fadeIn(.5*fadeSpeed);
				currentThumb.addClass('current');
				slidePointer(currentThumb);
			}
		},
	
		clickThumb = function(thumbLink){
			thumbLink.click(function(){
				thumb = $(this).parents('li'); 
				showSlide(thumb);
				//clearInterval(autoCycle);
				//autoCycle = setInterval(cycleAll, cycleSpeed);
				return false; 
			});
		},
	
			
		// Cycle selected slide
		cycleAll = function(){
			if(autoCycleOn){
				if (noThumbs > 4){
					if ($(thumbs + '.current').index() < 4) {
						nextThumb = $(thumbs + '.current').next();
					}
					else{
						nextThumb = $(thumbs).eq(0);
					}
					showSlide(nextThumb);	
				}
			}
		},
		
		//Auto-cycle Thumbs
		autoCycle = setInterval(cycleAll, cycleSpeed);
		
		// Pause the auto-cycle
		pauseAutoCycle = function(element){
			if(autoCycleOn){
				$(element).hover(function() {
					 clearInterval(autoCycle);
				}, function() {
					if (!killedAutoCycle){
						autoCycle = setInterval(cycleAll, cycleSpeed)
					}
				});
			}
		},
		
		// End the cycling if they click a slide, assuming that 
		// it's a video or they're leaving the page
		killAutoCycle = function(element){
			$(element).click(function() {
				killedAutoCycle = true;
				clearInterval(autoCycle);
			});
		};	
	
		$(init);
	}());
	
});
any help would be appreciated!

Last edited by cozart; 02-20-2013 at 09:32 PM..
cozart is offline   Reply With Quote
Old 02-19-2013, 04:52 PM   PM User | #2
billboy
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
billboy is an unknown quantity at this point
Where on your page is this;

Code:
<script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="scripts/sliderscript.js" type="text/javascript"></script>
it should be between <head> </head>
billboy is offline   Reply With Quote
Old 02-19-2013, 06:11 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by billboy View Post
it should be between <head> </head>
No it shouldn't.

With rare exceptions all JavaScript should go immediately before the </body> tag. If you put it in the head then it slows the loading of the page and you need extra code to delay running it until after the page has loaded.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
cozart (02-20-2013)
Old 02-19-2013, 06:55 PM   PM User | #4
billboy
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
billboy is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
No it shouldn't.

With rare exceptions all JavaScript should go immediately before the </body> tag. If you put it in the head then it slows the loading of the page and you need extra code to delay running it until after the page has loaded.
correct, my bad sorry for any confusion to the OP
billboy is offline   Reply With Quote
Old 02-20-2013, 09:30 PM   PM User | #5
cozart
New Coder

 
Join Date: Aug 2010
Posts: 10
Thanks: 4
Thanked 0 Times in 0 Posts
cozart is an unknown quantity at this point
moving those two scripts to before the </body> tag did the trick.

thanks!
cozart is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:19 AM.


Advertisement
Log in to turn off these ads.