-1

How to run javascript in hidden header to include and run external javascript files?

Using

document.createElement('script');
var js = documemnt.getElementsByTags('head')[0];
js.src = "file.js"; // File location
js.type = text/javascript;

and

head.appendChild(js);

to add to head section. I want to execute the javascript code in the similar way to add to head hidden behind to execute and .removeChild() to remove the code after code executed. Please tell me how to execute the code on hidden head.

Edited//
The example use ScriptManager , Referene:public final class ClientScriptManager, to handle the client side scripts to hide javascript and run, Call to javascript Function from code behind using ScriptManager.RegisterStartupScript.
There is no reference of it also, but with asp.net. Please tell me how to use them only on javascript without asp.net, "because it makes only slower to use asp without any commercial uses."

john3825
  • 587
  • 1
  • 6
  • 14
  • What does "on hidden head" mean? What you have there will work just fine. (Side note: You don't need the `type` property, the default is and always has been JavaScript.) – T.J. Crowder Sep 18 '13 at 12:55
  • As T.J. said, head section is hidden, on the other hand, scripts are not visible. Describe more about your question –  Sep 18 '13 at 12:58
  • I dare add to the above... sounds like the included file (js.src) is not found. What does firebug/developer tools' network tab show? – Paolo Stefan Sep 18 '13 at 13:02
  • possible duplicate of [Can't append – Mark Schultheiss Sep 18 '13 at 13:15
  • I read that you can add the script element to the header behind and run with adding dynamically. Please tell me how to add them. "_This question may already have an answer here: Can't append – john3825 Sep 19 '13 at 05:45
  • I added extra details about the question. – john3825 Sep 19 '13 at 10:17
  • @Paolo Stefan the firebug shows nothing executed with defined errors. I meant that executing the script on hidden `head` and remove the element and hidden `head` after the program execute, have to `head` section, ___one for user page and the other for execute the script___. – john3825 Sep 22 '13 at 07:03
  • @T.J. Crowder I was just asking how to execute a script on hidden `head` section and remove them. – john3825 Sep 22 '13 at 07:05
  • @KazuhiroYasui: *"have to head section, one for user page and the other for execute the script"* If you mean "two" rather than "to", note that [that is invalid HTML](http://www.w3.org/TR/html5/document-metadata.html#the-head-element). – T.J. Crowder Sep 22 '13 at 09:02

2 Answers2

2

Your code has a few typos, for instance you were not holding your new Node in a variable, and you were trying to append to an undefined variable.

var js = document.createElement('script'),
    head = document.getElementsByTagName('head')[0];
js.src = 'file.js'; // whatever
js.type = 'text/javascript';
head.appendChild(js);
head.removeChild(js);

In future, try opening your web console on the offending page and seeing if you get any error messages.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • I corrected the 3rd. line. It was meant an exampale. However,on lines 5-6 `head.appendChild()` and `head.removeChild()` cause the script to added to `head` dynamically on the user side page. How to execute the function on the `head` element __behind__ the user page and remove, preferably using `ScriptManager` or `XMLHTTPRequest()`? – john3825 Sep 22 '13 at 06:58
  • 1
    @KazuhiroYasui You mean like.. use _XMLHttpRequest_ to fetch, add the response as the contents of the ` – Paul S. Sep 22 '13 at 12:37
  • I mean javascript `scriptmanager` to call js function and during the execution of the js function it should be on behind. Then, remove the script after. – john3825 Sep 24 '13 at 05:05
0

Did you copy/paste your code or type it sloppily? It should be:

var head = document.getElementsByTagName( "head" )[ 0 ],
    js = document.createElement( "script" );
js.src = file;
js.type = "text/javascript";
head.appendChild( js );
prayerslayer
  • 883
  • 9
  • 13