www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: read sourcode before dom::ready

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    read sourcode before dom::ready

    hy all

    i have a small problem, i want to read the actual page source code before the dom is ready.

    i think the best place is the <head> tag to load my function.
    but inside the head, i cant find a way to read the raw source code.

    I tried
    Code:
    document.body.innerHTML
    .... yes i know the dom is not ready, the html not finaly parsed, so document.body is undefined :/

    now i search a way to read the source code.
    im open for all solutions

    Thanks all!

  2. #2
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234
    Javascript is available throughout the process, although some references are not available, but you can avoid those calls. If you manage to pack your document into a
    JS variable, avoiding newline characters that destroy the string, and using only one type of quotes within the document, then you can access the page data. However your server has to queue Browser output until the page is complete.
    <p>Suppose the final Server output is X, encapsulate it in JavaScript, making more output JS(X), subsequently the total output is JS(X) plus X, which means you will transmit twice the regular page length.

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    You can always place a script in the body to read some preceding part of the page...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head id="hdr">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Untitled</title>
    <style type="text/css">
    </style>
    </head>
    <body>
    <script type="text/javascript">
    alert(document.getElementById('hdr').innerHTML)
    </script>
    </body>
    </html>
    For the body an onload event is enough !

  4. #4
    Join Date
    Feb 2012
    Posts
    7
    yes sure, because the head is already loaded, i want this :

    Code:
    <html>
        <head>
            <title>Untitled</title>
            <script type="text/javascript">
                 alert(document.body.innerHTML)
             </script>
        </head>
        <body>
              <div class="whatever">i want to read this div</div>
         </body>
    </html>
    but this cant work because document.body is not rendered in the dom, so its empty. i search a way to read the RAW source code before the dom rendered

    thanks

  5. #5
    Join Date
    Feb 2012
    Posts
    7
    Quote Originally Posted by WyCnet View Post
    Javascript is available throughout the process, although some references are not available, but you can avoid those calls. If you manage to pack your document into a
    JS variable, avoiding newline characters that destroy the string, and using only one type of quotes within the document, then you can access the page data. However your server has to queue Browser output until the page is complete.
    <p>Suppose the final Server output is X, encapsulate it in JavaScript, making more output JS(X), subsequently the total output is JS(X) plus X, which means you will transmit twice the regular page length.
    yes thats a solution but not the best one , i have also think about to make a ajax request to window.location but i hope we find a better way like document.getRawSource()

  6. #6
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by WyCnet View Post
    Javascript is available throughout the process, although some references are not available, but you can avoid those calls. If you manage to pack your document into a
    JS variable, avoiding newline characters that destroy the string, and using only one type of quotes within the document, then you can access the page data. However your server has to queue Browser output until the page is complete.
    <p>Suppose the final Server output is X, encapsulate it in JavaScript, making more output JS(X), subsequently the total output is JS(X) plus X, which means you will transmit twice the regular page length.
    Definition of Newline

    Code:
    X = "Hello, this is a variable
    that is not a valid string as its 
    on three lines, containing a new lines.";
    
    Y = "Hello, this is a variable \r\nthat is a valid string as its\r\non three lines, containing the new line. characters";
    Quotes...

    Code:
    a = "\"This is Valid\"";
    b = "'This is also Valid'";
    c = '\'This is valid also\'';
    As long as you watching your quotes, they do not present a problem and where you have to use either a single or double quote with a back slash if you need to include it.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  7. #7
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Try this ...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Untitled</title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    function getSource(){
      var xmlHttpObj = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
      xmlHttpObj.open('POST',document.location,false);
      try{ xmlHttpObj.send();}catch(y){}
      var dt=xmlHttpObj.responseText;
     return dt;
    }
    alert(getSource());
    </script>
    </head>
    <body>
    Good luck !
    </body>
    </html>
    Works only on a server. Variant with this code for the script
    Code:
    (function getSource(){
       //...
     alert(dt);
    }());
    Last edited by 007Julien; 02-11-2012 at 07:16 AM. Reason: Complément, variant

  8. #8
    Join Date
    Feb 2012
    Posts
    7
    thanks, i think already about the ajax way, but i dont want to make a seperate request to get the >>local<< source code

  9. #9
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234
    JunkMale, I agree with you 100&#37; and more. However when assigning a JavaScript string like var YES ="text in here" or var NO = 'text in here', if text-in-there contain similar quotes as its encapsulation, then you arrive at broken strings at the browser level. However if text-in-there was recoded with backslashed quotes, then either quoting system could be used, and the 2nd text woukd have to be decoded at the browser level.

  10. #10
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Broken strings only crop up because of poor programming and attention to detail.
    The idea is to code in such a way that you do not need the \ at all and only when needed.
    Code:
    htmlCode = "<a href=\"#\" onclick=\"doSomething(this);\">click me</a>";
    // vs
    htmlCode = "<a href='#' onclick='doSomething(this);'>click me</a>";
    apart from the obvious clarity, the code is readable and less likely to be broken and when your looking to write in figure on the fly
    Code:
    htmlCode = "<a href=\""+urlVal+"\" onclick=\"doSomething(this);\">click me</a>";
    will always be better expressed as
    Code:
    htmlCode = "<a href='"+urlVal+"' onclick='doSomething(this);'>click me</a>";
    Escaping strings is nothing new neither is encapsulation.

    The choice is really down to the application needs and not the programmers preferences, although coding is a personal choice of style and format, people generally sit with what they know or are comfortable with.

    Your concerns of broken strings is over come by proper encapsulation and escaping where needed.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  11. #11
    Join Date
    Feb 2012
    Posts
    7
    eh dudes the discussion goes in the wrong direction i cant put the html code into a js var because the script is a standalone script. it means i want to use the code on different webpages without great backend that can fill the js var

  12. #12
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    The question is raised to "Why do you need the current page in a JS var?" the whole DOM is readily available to any script that runs in the browser, packing a page in to a variable is pretty pointless thing to do.

    It eats more resources than are really required, the memory is used also and I see no real justification for stuffing a variable with HTML, I can understand grabbing values out of the DOM and storing them of later use, but no reason I can conjure up as to the reason why the whole page needs to be in a variable.

    Your comment about a standalone script, sorry, no such thing, that is compiled languages and javascript is interpreted, big difference.

    It might help if you explain what the reason is for your need to have the page replicated in a JS variable. It may highlight an easier alternative to what your trying to achieve.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  13. #13
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by Sor3nt View Post
    i have also think about to make a ajax request to window.location but i hope we find a better way like document.getRawSource()
    To the best of my knowledge, there is no such solution.

    But if you describe your ultimate goal, perhaps we can suggest a different approach.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  14. #14
    Join Date
    Feb 2012
    Posts
    7
    Hehe

    ok i try to explain my goal.

    we are in the world of advertisement, i build for a client a google adsense script, this script need to know how many targets the webpages has.

    normally its a simple part, wait for dom ready, select all targets, count them, request advertisements.

    now we can't wait for dom ready because of performance issues, the script have a small "do..while" that waits for the (hardcoded dom-ids) elements. if its present then add the advertisement to the element (this happens all before dom ready, so its much faster!)

    the problem is if the webpage have different targets, at page load we have 5 targets and after a reload we have only 3 or less… at this moment i have no informations about how many targets i have, i can't count them.

    so my idea was to read the raw source code and explode all my target elements and count them there

    (puh hard work in english i hope all can understand my crazy school english ^^)

  15. #15
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    You don't need a variable to do what you need to do. Yes you will have to wait for the dom to become ready.

    Performance issues are generally down to poor implementation and often poor coding, routines that are resource hungry and by the description of the do{}while wait, that is certainly an indication of poor code, if you nned to wait for the DOM to become ready, you either use the onload listener in the HTML body tag or apply a window.onload listener that does the same thing and that is used to run your scrip when the dom is ready.

    any "ugly" elements should be masked by css hide if your using targets in a web page.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles