www.webdeveloper.com
Results 1 to 8 of 8

Thread: .appendChild trouble

  1. #1
    Join Date
    Oct 2003
    Location
    Pensacola, Fl
    Posts
    149

    .appendChild trouble

    Hi, I wondered if anyone might have a suggestion for me on this javascript snippet. I've used this code to write into an html page an array from an external source. I have not used this code for a long time, and found that when I looked at the website it had stopped functioning. Can anyone suggest what may be wrong with this that would keep it from loading properly now? In this example, "lookpath" is the path to the external .js file with the array I want to read into the html page.

    Code:
    e=window.document.createElement('script');
    	e.setAttribute('src',lookpath);
    	window.document.body.appendChild(e);
    Thanks for any suggestions - - Okham

  2. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,472
    Hi there okham,

    try it like this...
    Code:
    
       var e=document.createElement('script');
           e.setAttribute('src','lookpath.js');
    	 document.body.appendChild(e);

    coothead

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Depending on when it's run could rather be the problem. If this script is sitting in the document <head> before the body, then the <body> element won't be available (unless you're using the documents onload event or similar).

    Code:
    window.onload = function()
    {
    
    	e = window.document.createElement('script');
    	e.setAttribute('src',lookpath);
    	window.document.body.appendChild(e);
    
    };
    Alternatively the script could sit as a node within the <body>:

    Code:
    <body>
    
    	<script type="text/javascript">
    
    		e = window.document.createElement('script');
    		e.setAttribute('src',lookpath);
    		window.document.body.appendChild(e);
    
    	</script>
    
    </body>

  4. #4
    Join Date
    Oct 2003
    Location
    Pensacola, Fl
    Posts
    149
    Thanks for the suggestion coothead. That doesn't seem to be the problem. This is a script that is referenced in the index <head>, in an external file called "main.js". Maybe I should include more of the function. If you're interested, take a look at it and see if you notice what might now be hanging up the works...

    Code:
    	switch(parent.imgtype)
    	{
    		case "design":
    			lookpath="items/designs/des"+jumpdesno+".js";
    			lookpic.src="pics/designs/design"+jumpdesno+".gif";
    			break;
    		case "mono":
    			lookpath="items/monos/mono"+jumpdesno+".js";
    			lookpic.src="pics/monos/mono"+jumpdesno+".gif";
    			monodiv.style.display="block";
    			break;
    		default: break
    	}
    	e=window.document.createElement('script');
    	e.setAttribute('src',lookpath);
    	window.document.body.appendChild(e);
    	clr1.innerHTML=chng1[0];
    	clr2.innerHTML=chng2[0];
    	clr3.innerHTML=chng3[0];
    	clr4.innerHTML=chng4[0];
    	clr5.innerHTML=chng5[0];
    	clr6.innerHTML=chng6[0];
    	clr7.innerHTML=chng7[0];
    	clr8.innerHTML=chng8[0];
    	clr9.innerHTML=chng9[0];
    	clr10.innerHTML=chng10[0];	
    	clr11.innerHTML=chng11[0];
    	clr12.innerHTML=chng12[0];
    	clr13.innerHTML=chng13[0];
    	desnum=des[0];
    	desno.innerHTML=des[0];
    	size3.innerHTML=wxh3[0];size4.innerHTML=wxh4[0];size5.innerHTML=wxh5[0];size6.innerHTML=wxh6[0];

  5. #5
    Join Date
    Oct 2003
    Location
    Pensacola, Fl
    Posts
    149
    Thanks bionid. The script is linked to the index in the <head> by an external script file "main.js". I'll include more of the script here so you've got a clearer picture of what's happening...of course, it may be as you said that the <BODY> isn't available, though it was previously and it worked.

    Code:
    function makeitem(lookno)
    {
    	if(parent.jumpq==1)
    		{
    		switch(lookno)
    			{
    			case "No. Only":
    				parent.document.getElementById('look').style.display='none';alert("Enter design no., or Click another menu option."); jumpdesno=0; parent.djump.value="No. Only"; parent.jumpq=0;
    				break;
    			case "":
    				parent.document.getElementById('look').style.display='none';alert("Enter design no., or Click another menu option."); jumpdesno=0; parent.djump.value="No. Only"; parent.jumpq=0;
    				break;
    			case parent.djump.value:
    				if(lookno<0){parent.document.getElementById('look').style.display='none'; alert("Enter a number between '0' and "+nodes+", or Click another menu option."); jumpdesno=0; parent.djump.value="No. Only"; parent.jumpq=0;}
    				if(lookno>nodes){parent.document.getElementById('look').style.display='none'; alert("Enter a number between '0' and "+nodes+", or Click another menu option."); jumpdesno=0; parent.djump.value="No. Only"; parent.jumpq=0;}
    				break;
    			case parent.mjump.value:
    				if(lookno<0){parent.document.getElementById('look').style.display='none'; alert("Enter a number between '0' and "+nomonos+", or Click another menu option."); jumpdesno=0; parent.mjump.value="No. Only"; parent.jumpq=0;}
    				if(lookno>nomonos){parent.document.getElementById('look').style.display='none'; alert("Enter a number between '0' and "+nomonos+", or Click another menu option."); jumpdesno=0; parent.mjump.value="No. Only"; parent.jumpq=0;}
    				break;
    			default: break;
    			}
    		jumpdesno=lookno; parent.djump.value="No. Only"; parent.mjump.value="No. Only"; parent.jumpq=0;
    		}
    		else{lookpicno="pic"+lookno; jumpdesno=top.document.passform[lookpicno].value; parent.jumpq=0;}
    
    	switch(parent.imgtype)
    	{
    		case "design":
    			lookpath="items/designs/des"+jumpdesno+".js";
    			lookpic.src="pics/designs/design"+jumpdesno+".gif";
    			break;
    		case "mono":
    			lookpath="items/monos/mono"+jumpdesno+".js";
    			lookpic.src="pics/monos/mono"+jumpdesno+".gif";
    			monodiv.style.display="block";
    			break;
    		default: break
    	}
    	e=window.document.createElement('script');
    	e.setAttribute('src',lookpath);
    	window.document.body.appendChild(e);
    	clr1.innerHTML=chng1[0];
    	clr2.innerHTML=chng2[0];
    	clr3.innerHTML=chng3[0];
    	clr4.innerHTML=chng4[0];
    	clr5.innerHTML=chng5[0];
    	clr6.innerHTML=chng6[0];
    	clr7.innerHTML=chng7[0];
    	clr8.innerHTML=chng8[0];
    	clr9.innerHTML=chng9[0];
    	clr10.innerHTML=chng10[0];	
    	clr11.innerHTML=chng11[0];
    	clr12.innerHTML=chng12[0];
    	clr13.innerHTML=chng13[0];
    	desnum=des[0];
    	desno.innerHTML=des[0];
    	size3.innerHTML=wxh3[0];size4.innerHTML=wxh4[0];size5.innerHTML=wxh5[0];size6.innerHTML=wxh6[0];
    }

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    When is the function makeitem invoked? It is still a question of when this code is executed, and whether the <body> is available at the time.
    If you run the code in Chrome or FireFox, the console would report the error you're experiencing, particularly it would say the body element is null if not found.

  7. #7
    Join Date
    Oct 2003
    Location
    Pensacola, Fl
    Posts
    149
    I see what you're saying bionid. Well, the function is called in the <BODY>, as in...

    Code:
    <body onLoad="makeitem(parent.lookno);" bgcolor="#ffffff">
    So I think by what you're saying, BODY should be available to append to...I might add that this HTML file is loaded in an iFrame.

  8. #8
    Join Date
    Oct 2003
    Location
    Pensacola, Fl
    Posts
    149
    I might be able to find a solution if I approach the problem differently...I'll explain what I'm trying to accomplish...I have an iFrame on an index page that I load an html file into. In this file I include a script file called "main.js". Using onLoad I call a function out of main.js. In main.js I initialize a series of arrays. When I call this function called makeitem(), I try to read into the html file a subsequent array from an external file that I want to overwrite the first initialization. Basically I write into the innerHTML the values of the new array using appendChild. Originally this all worked, but perhaps changes in browsers has made my original code ineffective. If anyone could suggest a way to accomplish these tasks, feel free to reply. I know it's complicated...lol. Any examples of how this might be accomplished would be helpful, I'm sure. - - Okham

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles