0

i'm trying add external javascript code with this code:

function get_js(b){
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = b;
head.appendChild(script);
    }

This external javascript code using document.write inside:

document.write('<script language="JavaScript">document.write(\'<div id="something" style="position: fixed; top: 0; bottom: 0; left: 0; right: 0;">&nbsp;<\/div>\');...

When i'm trying execute function from this file, i'm getting this error:

ReferenceError: some_function is not defined

I thought that document.write in JS code is problem. Can anybody help me?

Thanks in advance!

///

OK, figured it out with POSTSCRIBE: https://github.com/krux/postscribe

2
  • You should avoid document.write altogether: stackoverflow.com/a/802943/3299157 Commented Mar 2, 2016 at 15:01
  • Yes i know, but this is 3rd party script. Commented Mar 2, 2016 at 16:19

1 Answer 1

2

don't use document.write(). If you call document.write() after the page is load, it will "reopen" the pageload and as a sideeffect clear everything from the screen.

take a look at properties/methods like innerHTML, textContent, document.createElement(), appendChild(), ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.