Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamically adding script tag to a DIV

Member
 
Join Date: May 2007
Posts: 63
#1: Jun 7 '07
Hey everyone,

I'm trying to add javascript tags into a specific DIV so that I can use a variable to change out the script tags source and use it as a type of "javascript-include"

I found a tutorial that made it look fairly simple, but I can not get it to work so far.

Here's the code for the DIV:
Expand|Select|Wrap|Line Numbers
  1.     <div id="myDepartmentDiv" class="myDepartmentDiv">
  2.         <span>
  3.  
  4.         </span>
  5.       </div>
  6.  
I use an alert function to check if the correct string is being sent, which it is sending the correct information and the variable of "whichDept" is being sent correctly as well which the alert message proves to be true.

Here's the code for the dynamic javascript insert:
Expand|Select|Wrap|Line Numbers
  1. function myWhichDepartmentFunction(whichDept,someVar1,someVar2) 
  2. {
  3.     if(whichDept=='1')
  4.     {
  5.         alert(someVar1);
  6.         var myDepartmentDivID = document.getElementsByTagName("myDepartmentDiv")[0];         
  7.         var newScript = document.createElement('script');
  8.         newScript.type = 'text/javascript';
  9.         newScript.onload=scriptLoaded;
  10.         newScript.src = 'js_files/' + someVar1 +'.js';
  11.         myDepartmentDivID.replaceChild(newScript);
  12.         var myDepartmentDivID2 = document.getElementsByTagName("myDepartmentDiv")[0];         
  13.         var newScript2 = document.createElement('script');
  14.         newScript2.type = 'text/javascript';
  15.         newScript2.onload=scriptLoaded;
  16.         newScript2.src = 'js_files/' + someVar2 + '.js';
  17.         myDepartmentDivID2.appendChild(newScript2);    
  18.     }
  19. }
  20.  
I have used this same logic before with href tags successfully but I can not get a script tag to work.

I have also attempted using innerHTML method:
Expand|Select|Wrap|Line Numbers
  1.     {
  2.         alert(someVar1);
  3.         var myDepartmentDivID = document.getElementsByTagName("myDepartmentDiv")[0];         
  4.         var newScript = document.createElement('script');
  5.         newScript = '';
  6.         newScript += "<script type=javascript src='" + someVar1 + "'.js></script>";
  7.         myDepartmentDivID.innderHTML = newScript;
  8.         var myDepartmentDivID2 = document.getElementsByTagName("myDepartmentDiv")[0];         
  9.         var newScript2 = document.createElement('script');
  10.         newScript2 = '';
  11.         newScript2 += "<script type=javascript src='" + someVar2 + "'.js></script>";
  12.         myDepartmentDivID.innderHTML = newScript2;    
  13.     }
  14.  
The thing that puzzles me, is that I use the exact same logic on another page, yet it works with a href tag. Why?

Expand|Select|Wrap|Line Numbers
  1. function showDetail(evt, k) 
  2. {
  3. //    showDetailFeaturedArtist(k);
  4.     div2 = document.getElementById("extraDetails2");
  5.     div2.innerHTML = "";
  6.     div2.innerHTML = "<table width=300px><tr><td>"
  7.     //alert(k);
  8.     for(k; k < musics.length ; k++)
  9.     {
  10.     if (musics[k].nodeType==1)
  11.         { 
  12.             Musicvar = musics.item(k);
  13.             // alert('getshere');
  14.            attr1 = Musicvar.getAttribute("songname");
  15.            attr2 = Musicvar.getAttribute("songurl");
  16.             attr3 = Musicvar.getAttribute("id");
  17.             div2.innerHTML += "<a href='#extraDetails"+ attr2 +"' target=_blank>" + attr1 + "</a><br>";
  18.                 if(attr3 == "endepisode")
  19.                 {
  20.                     break;
  21.                 }
  22.         }
  23.     }
  24.     div2.innerHTML += "</td></tr></table>";
  25. }
  26.  
And this last piece of code retrieves data from an xml file that I insert into the html div without refresh or submit or anything.

The first one should be no big deal. Can anyone see why the first or second set of code would not work?

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#2: Jun 7 '07

re: Dynamically adding script tag to a DIV


append it to the body instead of a div.

document.body.appendChild(whatever);
Newbie
 
Join Date: Jul 2007
Posts: 1
#3: Jul 18 '07

re: Dynamically adding script tag to a DIV


I have a similar item.

I am creating a set of nodes, and trying to add a self-referring event handler:

Expand|Select|Wrap|Line Numbers
  1. var objframespan = document.createElement("span");            
  2.  
  3. if( isIE )
  4.     objframespan.className = 'bbphotoimageframe';
  5. else
  6.     objframespan.setAttribute('class','bbphotoimageframe');
  7.  
  8. if( isIE )
  9. {
  10.   objframespan.onClick = function() { setActivePicture(objframespan)};
  11.   objframespan.onMouseOver = function() { setMouseOver(objframespan)};
  12.   objframespan.onMouseOut = function() { setMouseOut(objframespan)};
  13. }
  14. else
  15. {                
  16.   objframespan.setAttribute('onclick','setActivePicture(this);');
  17.   objframespan.setAttribute('onMouseOver','setMouseOver(this);');
  18.   objframespan.setAttribute('onMouseOut','setMouseOut(this);');
  19. }
Now, the non-ie code works in FF, Safari, and Opera, but did not work in IE. Nor does the IE code block above... I have resorted to using innerHTML, but as it is not compliant, I would really like an alternative...

Suggestions, anyone?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Nov 5 '07

re: Dynamically adding script tag to a DIV


onClick, onMouseOver and onMouseOut should be lower-case, e.g. onclick.
Reply