kwangbul
kwangbul

Reputation: 41

The way to change DOM before the DOM is loaded completely

I know Some programs (like grease monkey) can modify DOM just after the DOM is loaded completely.

But Is it possible during the DOM is loading? or before loaded?

Upvotes: 3

Views: 6878

Answers (2)

Florian Margaine
Florian Margaine

Reputation: 60737

Yes, it's possible.

Check out this example:

<div id="t">Some text</div>
<script>
    document.getElementById('t').textContent = "Other text";
</script>
<div>Yay!</div>

The script part will modify the DOM even though the "Yay!" div hasn't loaded. It can only do that because the <div id="t"> is already loaded, though.

This is the extent of how much you can do.

If you want to insert something while the DOM is loading, you can use document.write (this is the trick that most ads providers use to add the script tag wherever their snippet is added).

Upvotes: 1

Related Questions