0

So guys, I made a little script to put in Tampermonkey for my presentations, but I have a problem.

What I want is to swap document.write(), but I don't know which function I could swap to replace it so that it leaves the code cleaner and more organized.

Besides document.write() is also not considered a good programming practice.

leon
  • 13
  • 4
  • 1
    The easiest way for me is to use jQuery and use this kind of function : `$('#item_with_an_id").html(variable_of_content_to_put_inside)` – noxo. Oct 23 '19 at 21:02
  • Learn [DOM Manipulation](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction). – PM 77-1 Oct 23 '19 at 21:06
  • Alternatives: 1.) Use AJAX to replace DOM objects. 2.) take a look at the new – Thomas Ludewig Oct 23 '19 at 21:13

1 Answers1

0

You could try to come up with your own function. But before taking any action, you need to think that there must be a injection into document body. You can try to use

document.body.appendChild()

and inside off that method, you can have some kind of html code. Other than that you must generate a html document by using document.createDocumentFragment() or document.createElements().

hatirlatici
  • 1,598
  • 2
  • 13
  • 24
  • 1
    document.body.appendChild() not document.appendChild() – hatirlatici Oct 23 '19 at 21:31
  • `.appendChild()` takes a Node, not `.innerHTML`. Use `document.createElement()` to create an Element, then just assign properties with the dot syntax, before using `AnyNode.appendChild(Node)`. `document.createElements()` is not a function. – StackSlave Oct 23 '19 at 21:35