Injecting Javascript Not Working

Because of the way we are loading these pages, we want to use the DOM to inject <SCRIPT> variables and source...

function putJS(cid, pid) {
    var _line1 = '<scr' + 'ipt>var zflag_nid="607"; var zflag_cid="' + cid + '"; var zflag_sid="' + pid + '"; var zflag_width="1"; var zflag_height="1"; var zflag_sz="94";<\/script>';
    var _line2 = "<scr" + "ipt src='http://newt1.com/jsc/z5/fo.js'><\/script>";
    var _script = document.createElement("div");
    _script.innerHTML = _line1 + _line2;
    (document.getElementsByTagName("body")[0] || document.documentElement).appendChild(_script);
}

Open in new window


But, for some reason - its not working.

I've also tried:

function putJS(cid, pid) {
   var _script = document.createElement("script");
    _script.innerHTML = 'var zflag_nid="607"; var zflag_cid="' + cid + '"; var zflag_sid="' + pid + '"; var zflag_width="1"; var zflag_height="1"; var zflag_sz="94";';
    //_script.onload = scriptLoaded;
    var _scriptSource = document.createElement("script");
    _scriptSource.src = "http://www.com/fo.js";
    _script.async = true;
    _scriptSource.async = true;
    var docFrag = document.createDocumentFragment();
    docFrag.appendChild(_script);
    docFrag.appendChild(_scriptSource);
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(docFrag);
}

Open in new window


And I get an error about writing to the page...  

I'm not sure if there is anything I can do... I have no access to the fo.js script also, as its being loaded externally from a source we do not own.

Thanks
adworldmediaCTOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Here's a small example: http://jsfiddle.net/AlexCode/4RFg7/
function injectScript(arg1){
    var funct = "function main() { alert('injected: " + arg1 + "'); }";
    
    var scriptElem = document.createElement('script');
    scriptElem.appendChild(document.createTextNode('('+ funct +')();'));
    (document.body || document.head || document.documentElement).appendChild(scriptElem);
}

injectScript('parameter 1');

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
var sr = document.createElement("script")
sr.type = "text/javascript";
sr.src = "http://www.com/fo.js";
document.getElementsByTagName("head")[0].appendChild(sr);

Open in new window


your -
     _script.innerHTML
will fail, , as the  document.createElement("script")   has NO  "innerHTML"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.