Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    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>

  • #3
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    My apologies


    I had left the script tags in the js file.


    Thanks for your help guys it is working fine now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •