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
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS function won't work when placed in external file

    Hi folks, bit wet behind the ears with Javascript so if anyone can help me out here I'd be grateful.

    Basically, on a page I'm developing thumbnail pictures appear on the right-hand side of the page and the main picture appears over on the left. When the user clicks a thumbnail, the picture on the left changes to that thumbnail's full size.

    All of that is working fine, but the captions underneath are causing problems. In order to change the picture's caption as well, I've gotten the following code working when embedded in the page and the captions change perfectly.

    However, when I place the code in an external file, the captions won't change. I'm guessing it's because the changeIt2() function is invisible to the page, but that's only a (bad) guess. Can anyone offer any suggestions? Thanks in advance for any help.


    Code:
    <script language="javaScript" type="text/javascript">
    function changeIt2(objName2)
    {
    
    	var obj2 = document.getElementById(objName2);
    	var objId2 = new Array();
    
    	objId2[0] = "quote1";
    	objId2[1] = "quote2";
    	objId2[2] = "quote3";
    	objId2[3] = "quote4";
    	objId2[4] = "quote5";
    	objId2[5] = "quote6";
    	objId2[6] = "quote7";
    	objId2[7] = "quote8";
    	objId2[8] = "quote9";
    	objId2[9] = "quote10";
    	objId2[10] = "quote11";
    	objId2[11] = "quote12";
    	objId2[12] = "quote13";
    	objId2[13] = "quote14";
    	objId2[14] = "quote15";
    	objId2[15] = "quote16";
    	objId2[16] = "quote17";
    	objId2[17] = "quote18";
    	objId2[18] = "quote19";
    	objId2[19] = "quote20";
    	objId2[20] = "quote21";
    	objId2[21] = "quote22";
    	objId2[22] = "quote23";
    
    	var j;
    
    	for(j=0;j<objId2.length;j++)
    	{
    		if(objName2 == objId2[j])
    		{
    			obj2.style.display = "block";
    		}
    		else
    		{
    			tempObj2 = document.getElementById(objId2[j]);
    			tempObj2.style.display = "none";	
    		}
    
    	}
    
    	return;	
    }
    </script>
    Here's the code on the webpage where I call the changeIt2() function. As I was saying, it works prefectly when the above code is inserted directly into the page but I'd prefer to have an external file.


    Code:
    <div>
    <img src="images/characters/brian/cac8.gif" alt="brian marsh 4" onclick="document.swap.src='images/characters/brian/bgp8.jpg'; changeIt2('quote8');" />
    </div>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    I take it that your external .js file does not include

    <script language="javaScript" type="text/javascript">

    and </script>

    You would proably get more help if you avoided taking people for granted with "thanks in advance". The time to thank people is after they have assisted you, not before.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would, of course, have thanked anyone after their response as well, so kindly spare me the etiquette lesson. And no, my external file does not include the <script language="javaScript" type="text/javascript"> line.

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well it's sorted. Philip M, you were right on the money about the closing </script> tag which I'd missed at the very bottom. Thanks for your help.
    Last edited by commodorejim; 07-03-2007 at 04:08 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    No problem. Glad to have been able to assist.


  •  

    Posting Permissions

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