Robin Robin - 2 years ago 115
HTML Question

Replacing document.write()

I am trying to replace the

function in Javascript. Not completely sure how to do, as I don't know which element to write the HTML to.

Currently, the code javascript code is inserted to my page using:

<script src="//" type="text/javascript"></script>

The file contains a

document.write('<h2>Testing HTML</h2>Hello');

However, as
can't be called asynchronously, I need to figure out another solution. I can not use
or something like that, because I simply don't know any element on the target website.

I want the HTML code to be inserted exactly on the position that the script tag is.

Any suggestions?

Answer Source

Try this:

<script src="//" type="text/javascript" id="myScript"></script>

And in the javascript:

var script = document.getElementById('myScript');
script.outerHTML += '<h2>Testing HTML</h2>Hello';

As you don't have the script ID, try this:

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

// Async
setTimeout(function() {
    script.outerHTML += '<h2>Testing HTML</h2>Hello';
}, 2000);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download