0
<script type="text/javascript" src="something.js"></script>

something.js:

var scriptTag = document.getElementsByTagName('script');
scriptTag = scriptTag[scriptTag.length - 1];
var parentTag = scriptTag.parentNode;

This was recommended in https://stackoverflow.com/a/10312824/1099074.

However this solution doesn't work in certain cirumstances.

Same can be said of https://stackoverflow.com/a/3326554/1099074.

What would be an airtight solution that doesn't involve the frownable document.write? E.g. the solution I'm currently going with:

var childId = Math.floor((Math.random() * 1000000) + 1);
document.write('<div id="' + childId + '"></div>');
var parentTag = document.getElementById(childId).parentNode;

Help greatly appreciated!

Community
  • 1
  • 1
Wes
  • 1,183
  • 3
  • 23
  • 51
  • 3
    What are you trying to accomplish?! – 000 May 22 '14 at 23:17
  • I need to insert HTML using strictly Javascript directly below where the script tag that grabs the Javascript appears. – Wes May 22 '14 at 23:18
  • Why? What's the reason that you have to append something directly after the script tag? It could be in `` for all you know. Also what are the _"certain circumstances"_ in which the current solution doesn't work? – Etheryte May 22 '14 at 23:23
  • Because it's an embed code for an ad. So whever the customer places the embed code, the – Wes May 22 '14 at 23:25
  • We give them the – Wes May 22 '14 at 23:30
  • @Joe document.scripts[document.scripts.length - 1] doesn't work. I am already using document.write, but from what I hear it is frownable. – Wes May 22 '14 at 23:31
  • In what circumstances doesn't `document.scripts[...]` work? I'm yet to encounter such a situation. – Etheryte May 22 '14 at 23:32
  • 1
    Can you explain under what circumstances your solution in `something.js` does not work? In this JSFiddle I use that + `appendChild` and it seems fine: http://jsfiddle.net/YC7TZ/ – christian314159 May 22 '14 at 23:35
  • @Nit I'm not sure exactly, but it clearly isn't working sometimes in my test instance. I'm not sure exactly of the cause. It works most of the time however. – Wes May 22 '14 at 23:49
  • Find out replicable conditions under which it doesn't work and then work on solving the problem rather than trying to reinvent the wheel. – Etheryte May 22 '14 at 23:51

1 Answers1

0

Here is something I wrote in the past that accomplishes this. It does not assume that jquery exists, so it contains its own document-ready check. The trick is that it should wait until the document is loaded, then go back and add the element.

(function () {

    var current_script = document.getElementsByTagName('script');
    current_script = current_script[current_script.length - 1];

    // modified from http://dustindiaz.com/smallest-domready-ever
    function ready(callback) {
        /in/.test(document.readyState)
        ?setTimeout(function(){ready(callback)},9)
        :callback()
    }

    ready(function() {
        var div = document.createElement('div');
        div.setAttribute('id', Math.floor((Math.random() * 1000000) + 1));
        current_script.parentNode.insertBefore(div, current_script.nextSibling);
    });

})();

A simpler solution would be: Take control of the filename of your embeddable script. You could give the script a filename of ad_embed_f4t4igjf8ig93.js, and have the user embed that onto their page. On document-ready, just look for script tags with that src.

000
  • 26,951
  • 10
  • 71
  • 101