1

I call function with document.write() to add inline content (ad banner):

<body>
Page content start<br>
    <script>
    function postContent(){
      document.write('added content');
    }
    postContent();
    </script>
<br>Page content end
</body>

on page I got:

Page content start 
added content
Page content end

I want to add this content with delay, but inside setTimeout() document.write() overwrite all content.

<body>
Page content start<br>
<script>
    function postContent(){
        document.write('added content');
    }
    setTimeout(function () {
        postContent();
    }, 3000);
</script>
<br>Page content end
</body>

on page within 3s I got:

added content

How can I call function with document.write() with delay and do not overwrite all page?

Note: I do not have access to function that insert ad banner.

2 Answers2

1

If you run document.write after the document is loaded - it overwrites the entire document anycase. So, you should select a specific element, and add inner html text into it:

document.getElementById("container").innerHTML="added content";
Sergey Mell
  • 7,780
  • 1
  • 26
  • 50
1

You could use a span and write the line to it using innerHTML.

write() function works the first time because it loads the same time as the document so the line is added in between.

The second time the document is reloaded deleting all the html.

<body>
Page content start<br>
<span id="pid"></span>
<script>
    function postContent(){
        document.getElementById("pid").innerHTML='added content';
    }
    setTimeout(function () {
        postContent();
    }, 3000);
</script>
<br>Page content end
</body>
DroidNoob
  • 1,123
  • 9
  • 21