SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Empty page using jQuery

    Hello

    Is there a way through jQuery of JS in general to totally empty the output of a browsed document?
    That means removing the whole <html> children along to what could come above the <html> tag?

    Thank you

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    var html = document.documentElement;
    while (html.hasChildNodes()) {
        html.removeChild(html.firstChild);
    }
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    Code JavaScript:
    var html = document.documentElement;
    while (html.hasChildNodes()) {
        html.removeChild(html.firstChild);
    }
    I guess this will only remove the the html children not everything (assumingly i have something like
    Code:
    xyz <html>....</html>
    I'm trying to use the $(document).empty(); which is working perfectly; however there is a problem.

    What I have is as follows:
    1. PHP file runs, echoes something
    2. JS clears output at the beginning
    3. PHP output text later in the file

    the problem is, the empty is clearning everything, so whatever is to be outputted is removed too because it is executing at a later stage it seems.

    So the question now is, how can I let it run on an event such as it clears before the document is ready allowing further text to be output, or how can I select what is around the HTML tag? (I need to remove some text that is being output before my actual html).

    $('whatisaroundthehtml').empty();


    thank you

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zeez View Post
    I guess this will only remove the the html children not everything (assumingly i have something like
    Code:
    xyz <html>....</html>
    You can't. That's invalid HTML, and since the <html> start tag and the </html> end tag (plus those for head and body) are optional, browsers will interpret that as,
    Code:
    <html>
      <head></head>
      <body>
        xyz
        <html>....</html>
      </body>
    </html>
    The 'outer' HTML element is the document root element, so my code would still work.

    There can't be anything 'around' the HTML element. You can have a doctype declaration, SGML comments and, in theory at least, processing directives outside the HTML element, but nothing can wrap around it.

    This doesn't apply for XHTML served as an application of XML, but it does apply for HTML – including pretend-XHTML served as text/html.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I agree with you on that, but I have #!/usr/bin/php -q (it is needed for something) on the top of my PHP file, and I need to remove this line when executing the file through the browser.

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should then look into PHP's output buffering mechanism. You might have to wrap things in an extra layer of PHP files, but it shouldn't be impossible.

    Using ob_clean() you could clean the output buffer after the 'magic' line.
    Birnam wood is come to Dunsinane


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
  •