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 09-25-2008, 09:21 PM   PM User | #1
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Linking to external JS Not Working??

Hi Guys


I know very little Javascript but have found a script I would like to use from Dhtml goodies website. Basically it expands boxes to show content. I like the idea, but I don't want the big chunk of code on my web page, and would rather link to an external file.

Could anybody see if this is possible for this code.

Here is the code:
Code:
// JavaScript Slide Code
/************************************************************************************************************
(C) www.dhtmlgoodies.com, November 2005
************************************************************************************************************/

var dhtmlgoodies_slideSpeed = 10;	// Higher value = faster
var dhtmlgoodies_timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
var dhtmlgoodies_slideInProgress = false;
function showHideContent(e,inputId)
{
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('dhtmlgoodies_a' + inputId);
	var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			dhtmlgoodies_activeId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}
function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='dhtmlgoodies_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'dhtmlgoodies_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'dhtmlgoodies_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='dhtmlgoodies_answer_content';
			contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}
window.onload = initShowHideDivs;
If anybody could help me solve this issue that would be great.

Cheers Danny
winnard2008 is offline   Reply With Quote
Old 09-25-2008, 09:45 PM   PM User | #2
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
save the entire thing in a .js file, and then call it in the <head> section of your doc with something like:

Code:
<script src="myjavascript.js" type="text/javascript"></script>
ohgod is offline   Reply With Quote
Old 09-26-2008, 09:34 AM   PM User | #3
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
I have already tried that.

When I do this the expanding bit stops working. So I imagine there must be something in the code I need to change in order for it to work externally.

I no very little javascript but something in the code must be set for when the page loads the script works. Like an onload function or something.


I have no clue.

I just wanted to trim my page of as much code clutter as possible. If anybody can help me get rid of this chunk of code to external page and it still work then that would be great.
winnard2008 is offline   Reply With Quote
Old 09-26-2008, 10:34 AM   PM User | #4
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Ensure that on your external JS, you don't have this line of code:
Code:
<script....>
.
.
.
</script>
If nothing works, please provide a link to the page in question.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 09-26-2008, 04:50 PM   PM User | #5
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
My apologies


I had left the script tags in the js file.


Thanks for your help guys it is working fine now.
winnard2008 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 04:40 AM.


Advertisement
Log in to turn off these ads.