...

View Full Version : Linking to external JS Not Working??



winnard2008
09-25-2008, 10:21 PM
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:


// 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

ohgod
09-25-2008, 10:45 PM
save the entire thing in a .js file, and then call it in the <head> section of your doc with something like:


<script src="myjavascript.js" type="text/javascript"></script>

winnard2008
09-26-2008, 10:34 AM
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.

rangana
09-26-2008, 11:34 AM
Ensure that on your external JS, you don't have this line of code:


<script....>
.
.
.
</script>


If nothing works, please provide a link to the page in question.

winnard2008
09-26-2008, 05:50 PM
My apologies


I had left the script tags in the js file.


Thanks for your help guys it is working fine now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum