Appending script to a document with Javascript.

Discussion in 'JavaScript' started by blueparukia, Jun 14, 2008.

  1. #1
    var script = document.createElement('script');
    
    script.setAttribute('type','text/javascript');
    script.innerHTML = "var theRoot = document.getElementById('"+ pageName +"');var theHandle= theRoot.childNodes[0]; Drag.init(theHandle, theRoot);"
    
    outside.appendChild(script);
    Code (markup):
    Is what I currently have - which works in anything but IE and its stupid lack of DOM methods.

    I have tried just adding it with innerHTML, but that stops it working in any browser - so what is the best (if any) way to do this?
     
    blueparukia, Jun 14, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var data = "var theRoot = document.getElementById('"+ pageName +"');var theHandle= theRoot.childNodes[0]; Drag.init(theHandle, theRoot);";
    if (window.ActiveXObject) //ie
    	script.text = data;
    else
    	script.appendChild document.createTextNode(data));
    PHP:
    I don't see why you are doing it like this though.
     
    MMJ, Jun 14, 2008 IP
    blueparukia likes this.
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Awesome, thanks.
     
    blueparukia, Jun 14, 2008 IP