Control the volume of a document.write request from a third party

I am writing a web page that relies on an external javascript file (which I do not control) that returns data using document.write. Is there a way to dynamically call a function without overwriting the entire document? Here's the most concise code I can come up with:

<html> <head> <script type="text/javascript"> function horriblefunction () { document.write("new text"); } </script> </head> <body> Starting Text... <div id="pleasewriteinme"></div> Other text... <button onclick="horriblefunction();">Click</button> </body> </html> 

The idea begins with the fact that without changing the "horriblefunction ()" (as an external) new text can be placed in a div instead of rewriting the page. Is this possible or should the function be called inside the div when creating the page?

thanks for the help

+4
source share
2 answers

The only way to use document.write after the page has finished rendering is to temporarily replace the function with one of your own solutions that will insert content into the div. For instance.

 function horriblefunction() { var old_dw = document.write; document.write = function(text) { document.getElementById( 'some_div' ).innerHTML = text; } // now call your external JS function that uses document.write document.write = old_dw; } 

This will work as long as the external JS is already loaded and you just call the function. If you need to load JS (say by inserting a new <script> in the DOM), remember that this operation is asynchronous, and you will need to see how the DOM finds out when it is safe to restore the old version of document.write .

+5
source

Try using dynamic loading script from http://bezen.org/javascript/index.html

bezen.domwrite.js - Captures document.write and writeln to securely load external scripts after page loading.

+2
source

Source: https://habr.com/ru/post/1311636/


All Articles