Clearing a document in js

Discussion in 'Software' started by Dr_Xem@cs, Oct 10, 2007.

  1. Dr_Xem@cs

    Dr_Xem@cs Private E-2

    Hi everyone!

    I have a little question about the window.document object when javascript is invoked in a browser...

    I have tried (but failed) to find a function that clears the document and starts writing from the top. This is not necessary in Internet Explorer, since it is done automagically, but more so in Firefox, where every write() or writeln() appends to the old document regardless close() and open().

    Does anyone know about this quandry? Any help is appreciated!

    Doc.
     
  2. PC-XT

    PC-XT Master Sergeant

    I tried that problem before. Maybe my solutions can help? If it is another window or in a frame, you can set the location to "about:blank" to clear the page. (Actually, I heard that some browsers display some text in that "blank" document, so you may want to use a url to a blank html document instead.) This doesn't work if you are writing to the same window/frame that the script is in, because the script then stops. I ended up reloading the page with a query code when this was the case. (That is, setting document.location.href to the page's url, plus a ? query.) The script would check location.search to see if there was a query, and write the relevant info. This made it appear like the page cleared, and the page is usually still in the user's cache, so it doesn't take that long to load, either. This gets complicated if you use PHP or something that already uses the query. I was just using HTML documents. I didn't get much further than that. Hope it helps...
     
  3. JustAnotherDude

    JustAnotherDude Private E-2

    If all you want is HTML with some javascript, it's easy to clear the page. Inside the BODY tags, use a DIV, and just clear everything inside the DIV.

    Example:

    <HTML>
    <HEAD>
    <TITLE>Clear the Page</TITLE>
    <script language="javascript" type="text/javascript"><!--
    function clearme() {
    document.getElementById('dbod').innerHTML = '';
    }
    // -->
    </script>
    </HEAD>

    <BODY bgColor="#ffffff" text="#000066">
    <div id="dbod">

    <br> This page has some text on it, though nothing useful.
    <br> Below is a table, to demonstrate that not only text will be cleared...<br><br>
    <TABLE align="center" border=3 cellPadding=0 cellSpacing=0 width="50%">
    <TR><TD bgColor="#ddffdd">This is a test</TD><TD bgColor="#ddddff">This is a test</TD></TR>
    <TR><TD bgColor="#ffdddd">This is a test</TD><TD bgColor="#ddffdd">This is a test</TD></TR>
    <TR><TD align="center" colspan=2><br><br><a href="#" onClick="clearme();">Click Me, Baby!</a><br>&nbsp;</TD></TR>
    </TABLE>

    </div>
    </BODY>
    </HTML>
     
  4. Dr_Xem@cs

    Dr_Xem@cs Private E-2

    Hi JustAnotherDude

    This <div> stuff seems a good way around. It was not what I was looking for, but it suffices;) Thank You!

    Doc.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds