Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Document.write and pausing

  1. #1
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,734
    Thanks
    46
    Thanked 216 Times in 209 Posts
    Blog Entries
    53

    Default Document.write and pausing

    Hello everyone,
    I'm playing around with document.write (just for the fun of it). My question is about the following code.
    Code:
    <iframe src="some_file.html" name="probe" onload="external_html=frames.probe.document.body.innerHTML"></iframe>
    <script>
    alert();document.write(external_html)
    </script>
    The alert pauses parsing until we click on OK. During that brief moment of time, the iframe will have loaded, implying that external_html is 'known' after the click on OK. The result is a page that does not only contain the iframe, but also the innerHTML of the iframe (outside of it). And since we used document.write, every bit of code written in the iframed page will be automatically tranferred to the containing page (and will execute!).
    If we leave out the alert, document.write will achieve nothing, because external_html is not known yet at the time document.write is called. A timer won't correct this in a proper manner, because the whole page would be rewritten (and the iframe would not be visible anymore; this also applies to all other content).
    In other words, a timeout is not a good replacement of the alert. I tried a javascript sleep function: no result either. So my question: is there some piece of code that does exactly what the alert does except for the appearance of the alert box?
    Note: I'm not interested in solutions that use Ajax / that don't use the native document.write.
    Thanks.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    You could try a while or a for loop, but I suspect there might be issues with that:

    Code:
    <iframe src="some_file.html" name="probe" onload="external_html=frames.probe.document.body.innerHTML"></iframe>
    <script>
    while(typeof external_html === 'undefined'){
    	var who_cares = "knows what?";
    }
    document.write(external_html)
    </script>
    Like it might still wipe out the iframe, or it might complain that there's too much looping or recursion going on and ask if you want to continue, which would be a lot like an alert.

    But I suppose it's worth a try.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,734
    Thanks
    46
    Thanked 216 Times in 209 Posts
    Blog Entries
    53

    Default

    Quote Originally Posted by jscheuer1 View Post
    You could try a while or a for loop, but I suspect there might be issues with that.
    Yes, the browsers complains that there's too much looping. Maybe I should try to solve the issue using pure javascript only, since javascript is synchronous by nature.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    That was pure javascript. And I know you don't like AJAX, but consider that jQuery allows one to (via AJAX) import and execute javascript. And that of course that means it can be done without jQuery, it's just a lot more complex to write and understand.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,734
    Thanks
    46
    Thanked 216 Times in 209 Posts
    Blog Entries
    53

    Default

    John, what I ment was that the onload in the iframe calls the execution of javascript; it's not javascript itself. If it were js in itself, then in
    Code:
    <iframe src="some_file.html" name="probe" onload="external_html=frames.probe.document.body.innerHTML"></iframe>
    <script>
    document.write(external_html)
    </script>
    the execution of the onload in the iframe should be synchronous: it should occur before the document.write in the subsequent script, not after it. (And as the onload happens after the document.write, the result is 'nothing').

    I don't dislike Ajax. I'm just curious to know if we can simulate it with the help of an iframe (or other means).

    Some time ago, I created a HttpRequest that imports external content and executes code contained in it, and a iframe-simulation of the request that does exactly what is done by the HttpRequest, see this and this. But these scripts don't recognize code (contained in the external file) that is not explicitly marked as 'script', like <a onclick="alert('test')">test</a>. I know this issue is very well handled by the jQuery load method, but I wanted to find a solution that is not dependent on a library, just for the fun of it. That's why I was thinking of a document.write application.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    But you can't do doc write onload without wiping out the iframe because that invariably occurs after the iframe is parsed. Well It might work sometimes, though it's hard to image the iframe loading before the page is parsed. You just need to get in all your doc writing before the page parser (browser) closes the window for parsing/writing (before document ready).

    And it's really not a question of synchronous/asynchronous. It's just what I just said. You cannot doc write after doc ready without wiping stuff out.

    I'd go a bit further in that the reason there's no pausing in javascript without user interaction is, because (I think at least) if there were, you could hang stupid people up indefinitely.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,094
    Thanks
    1
    Thanked 205 Times in 201 Posts

    Default

    Hi there molendijk,

    check out the attachment, which gives a possible "document.write()" solution.

    Note that "Google" and "Opera" do not work locally but only on a server...

    Uncaught DOMException:
    Blocked a frame with origin "null" from accessing a cross-origin frame

    Also note that "document.close()" should always be used.
    It stops the window from hanging

    coothead
    Attached Files Attached Files
    ~ the original bald headed old fart ~

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    I thought of that, but it's not really a solution. The content is still zapped, you're just replacing it later. You would have to rewrite the entire page. At least as I understood it, the iframe was a stand in for an entire page. One could take the innerHTML of the entire page though, get rid of the load event on the iframe, then write everything including the imported content, but that seems stupid to me, and could have unforeseen consequences on complex pages.

    Arie might like it though . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,734
    Thanks
    46
    Thanked 216 Times in 209 Posts
    Blog Entries
    53

    Default

    Coothead, thanks very much. But your code wipes out all content on the page (except for the iframe-content). So it is not a real solution.
    Thanks anyhow.

  10. #10
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,094
    Thanks
    1
    Thanked 205 Times in 201 Posts

    Default

    Hi there molendijk,

    But your code wipes out all content on the page...
    Well, I was only working with the code that you provided.

    If you want a complete valid document example, then, no problem, just try version two.

    coothead
    Attached Files Attached Files
    ~ the original bald headed old fart ~

  11. The Following User Says Thank You to coothead For This Useful Post:

    molendijk (12-03-2016)

Similar Threads

  1. document write (JS)
    By d-machine in forum JavaScript
    Replies: 1
    Last Post: 01-21-2011, 06:21 AM
  2. Resolved using document.write to write a script tag
    By traq in forum JavaScript
    Replies: 4
    Last Post: 11-16-2010, 04:28 AM
  3. Document.write other
    By bluewalrus in forum JavaScript
    Replies: 2
    Last Post: 11-02-2009, 06:15 AM
  4. document.write help
    By Rachele7 in forum JavaScript
    Replies: 5
    Last Post: 03-10-2009, 07:15 PM
  5. using document.write()
    By Wizard13335 in forum JavaScript
    Replies: 4
    Last Post: 11-05-2005, 02:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •