1

Possible Duplicate:
JavaScript - controlling the insertion point for document.write

I want to delay document.write if block with document.write not in the viewport of browser. I tried to make it after window loaded, but document.write replaces the whole content :(

$(function(){
    document.write('bla')
)}

Also, in this document.write I have script with ads and it must work when added in block.

Is it possible?

UPD: i have code whith document.write, which pushes in document <script>. so, because of this code my page loads very slowly and i want to load this script only when dom is ready or when user srolls to the block where my ads placed

Community
  • 1
  • 1
rsboarder
  • 4,592
  • 3
  • 20
  • 21
  • Can you please rephrase your question? It is totally not clear: "I want to delay document.write if block with document.write" what?, "document.write replaces the hole content" thats the purpose of document.write... – RvdK Nov 03 '11 at 07:20
  • 1
    `window.setTimeout( fun_name, sec );` can use but your questions is abstract. – run Nov 03 '11 at 07:24

4 Answers4

3

document.write() will implicitly call document.open() which will clear your page (if it has finished loading).

You should only use it where it will be executed as the page loads.

alex
  • 479,566
  • 201
  • 878
  • 984
  • in `document.write` i have scripts which are loading from other resources. sometimes this resources very slowly so thats why i want to delay `document.write` and fire it after page is loaded – rsboarder Nov 03 '11 at 07:37
  • 1
    @rsboarder: You cannot delay `document.write`, that's just impossible. Maybe you could block the browser until the all your resources are loaded and prevent the document from being rendered completely. But it is more likely that the browser will abort the JavaScript before and that the user will be very annoyed. – Felix Kling Nov 03 '11 at 09:01
0

I think what you want is something like this, instead of document.write:

$('#MyDiv').html("My Text <strongn>My Bold Text</strong>");
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
miahelf
  • 784
  • 1
  • 7
  • 13
  • He's asking about ads. Which usually have several external scripts with document.write that you're not in control of. – gregers Jun 11 '12 at 08:17
0

It is very difficult. Because if you don't "write" the elements, the page body area will not have the correct size, and you will can not move/scroll you page. If you can measure the elements before written, you could add same space to the page body and then, you could get scroll bar. You also need some code to detect the scroll bar position and determine writing your elements.

ControlPower
  • 610
  • 4
  • 7
0

Remove the document.write from your code. Create a script DOM Element and append it to the head.

$(function(){
    var script = document.createElement('script');
    script.src = // Put the src of script here
    script.type = "text/javascript";
    document.getElementsByTagName('head')[0].appendChild(script);
)};
Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38