0

I am trying to add a script to the head tag of my page. I am seeing the code that I am trying to add in the body tag but it doesn't show up in head tag.

$(document).load(function() {
   $('<script>console.log("hi");</' + 'script>').appendTo("head")
});

I see this being added in <body> but I dont see it in <head>.

I tried variations of append, appendTo and document.ready and document.load functions. None of those worked so far.

I went through cpuple of similar threads before I posted. I didn't seem to fix my issue

user3861559
  • 973
  • 8
  • 25
  • 43

3 Answers3

0

One thing you might need to do is escape the </ as <\/

If its an inline script though maybe you can try just adding a setTimeout to it.

0

Try the following function:

(function (src) {
    var tagName = 'script', script = document.createElement(tagName);
    script.src = src;
    var elm = document.getElementsByTagName(tagName)[0];
    elm.parentNode.insertBefore(script, elm);
})('SCRIPT.SRC.HERE.js');

This will create a dynamic script tag and add it before the first script tag. Although it won't work in the way you want if you don't have any script tags inside your head. In this case, you could use the following function:

(function (src) {
    var tagName = 'script', script = document.createElement(tagName);
    script.src = src;
    var head = document.getElementsByTagName('head')[0];
    head.insertBefore(script, head.childNodes[0]);
})('SCRIPT.SRC.HERE.js');

This will directly get the first element inside your head tag and append the new script before it.

Romulo
  • 4,896
  • 2
  • 19
  • 28
0

The document doesn't fire a "load" event - or if it does, it's a bubbled event from something else loading (like another script or image file). Try changing document to window:

http://jsfiddle.net/5bhmwyrx/3/

$(window).load(function() {
   var script = $('<script>console.log("hi");</' + 'script>').appendTo(document.head);
   console.log("Script was appended to", script[0].parentNode.nodeName);
   //-> Script was appended to HEAD
});

$(document).load(function() {
   console.log("You should never see this");
});

document.onload = function () {
    console.log("You should not see this in modern browsers");
};
Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
  • Thanks, but still I dont see it in head tag – user3861559 Oct 06 '15 at 19:06
  • if you look at this, it does fire an onload event like window does (see: http://stackoverflow.com/a/588048/3769412) but it helps that the OP is using jQuery to deal with this, anyway which would smooth out anything and isn't answering the question OP asked anyway – Daemedeor Oct 06 '15 at 19:17
  • @Daemedeor - `document.onload = function () { console.log("document onload event"); };` doesn't log anything (in current browsers). That's so old it's not even implemented in browsers any more and was the JS api to `` - super super old school. – Ryan Wheale Oct 06 '15 at 19:25
  • @user3861559 - Did you try my fiddle?? Here is proof that it's being appended to the HEAD (look at the console): http://jsfiddle.net/5bhmwyrx/2/ – Ryan Wheale Oct 06 '15 at 19:26