Javascript: Page keeps loading

This is a discussion on Javascript: Page keeps loading within the Tech Board forums, part of the Community Boards category; I have two HTML pages. The first one (index.html)has a button that will open a new window (page.html). Code: <!DOCTYPE ...

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Javascript: Page keeps loading

    I have two HTML pages. The first one (index.html)has a button that will open a new window (page.html).
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>My Site</title>
    <script>
    function newWindow() {
      window.open( "page.html", "My Page", "width=200,height=200" );
    }
    </script>
    </head>
    <body>
    <h1>Title</h1>
    <p>some paragraph</p>
    <input type="button" value="New" onclick="newWindow()" />
    </body>
    </html>
    The opened window (page.html) has a button that will erase the content of parent window and write new content
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Page</title>
    <script>
    function eraseMe() {
      var doc = window.opener.document;
      var kids = doc.childNodes;
      for( var i = 0; i < kids.length; ++i ) {
        doc.removeChild( kids[ i ] );
      }
      
      window.opener.document.write( 
        '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
        '<html>' +
        '<head></head>' +
        '<body>' +
        '<p>Mamma mia</p>' +
        '</body>' +
        '</html>'
        );
     
    }
    </script>
    </head>
    <body>
    <input type="button" value="Erase" onclick="eraseMe()" />
    </body>
    </html>
    Everything works fine except after "eraseMe()" is executed, the browser won't stop loading. New content is written, yet the hour glass stall half way and the loading icon keeps endlessly turning; like if there was an infinite loop. So, any of you spot anything?
    Thanks in advance.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    Do you get any errors (lower left corner in IE, javascript console in Opera)?

    Also check the source of the open document to see if all the source you expect is present. I personally would not use pop-ups in any manner since so many people block them, but whatever. It's your site.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
      for( var i = 0; i < kids.length; ++i ) {
        doc.removeChild( kids[ i ] );
      }
    I doubt this is causing the problem but sometimes stepping back is better than stepping forward.
    Code:
      for( var i = kids.length - 1; i >= 0; --i ) {
        doc.removeChild( kids[ i ] );
      }

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Oh, the reverse loop will only make the loop faster, so I read.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by anonytmouse
    Code:
      for( var i = 0; i < kids.length; ++i ) {
        doc.removeChild( kids[ i ] );
      }
    Won't length change everytime an element is removed? My suggestion is to save the initial value of length to a variable and use that variable to control the for loop.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Good point, dude. And also the index changes as you remove. So here's adjustment
    Code:
      var len = kids.length;
      for( var i = 0; i < len; i++ ) {
        doc.removeChild( kids[ 0 ] );
      }
    But, apparently, it is the document.write() that causes the endless loading. Hmmm...

    And only in Firefox, hmm...
    No error message btw
    Last edited by alphaoide; 05-11-2005 at 12:42 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Add a call to document.close() after you have finished writing.

    >> Good point, dude. And also the index changes as you remove. <<

    Which is why I suggested stepping backwards. Your solution also works but compare what happens if one of the calls to removeChild fails. If possible, a cascading failure mode should be avoided.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I see. I did not know your intention when you suggested that. Also the close() function "closes" this issue. Thanks a lot.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. JavaScript book recommendations
    By neandrake in forum Tech Board
    Replies: 2
    Last Post: 04-05-2009, 12:27 PM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. web page loading ...
    By twomers in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2006, 12:42 PM
  4. virtual memory
    By sweets in forum C Programming
    Replies: 6
    Last Post: 11-06-2004, 05:55 AM
  5. Page loading times
    By C_Coder in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-07-2001, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21