Dumping content of included .js

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nick

    Dumping content of included .js

    Is it possible to dump the source code from a .js file and save it to a
    string? Something like:

    <head>
    <script language="JavaS cript" src="http://www.abc.com/test.js"
    type="text/javascript"></script>
    <script>
    function dumpScript() {
    var scripts = document.getEle mentsByTagName( 'SCRIPT');
    alert(scripts[0].source);
    }
    </script>
    </head>

    <body>
    <button onclick="dumpSc ript()">DUMP SCRIPT</button>
    </body>

    Thanks in advance!

  • Randy Webb

    #2
    Re: Dumping content of included .js

    nick said the following on 11/8/2006 6:39 PM:
    Is it possible to dump the source code from a .js file and save it to a
    string? Something like:
    No, the closest you could come would be to retrieve the file using an
    XMLHTTPRequest Object (AJAX in buzz words) and then read the
    responseText from the file.

    Opera9 will let you read the script elements .text property:

    document.script s[0].text

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

    Comment

    • RobG

      #3
      Re: Dumping content of included .js


      Randy Webb wrote:
      nick said the following on 11/8/2006 6:39 PM:
      Is it possible to dump the source code from a .js file and save it to a
      string? Something like:
      >
      No, the closest you could come would be to retrieve the file using an
      XMLHTTPRequest Object (AJAX in buzz words) and then read the
      responseText from the file.
      >
      Opera9 will let you read the script elements .text property:
      >
      document.script s[0].text
      Firefox has the ViewSourceWith extension (among others) - maybe less
      convenient but provides a few more features :-)

      <URL: https://addons.mozilla.org/firefox/394/ >

      --
      Rob

      Comment

      • Lich_Ray

        #4
        Re: Dumping content of included .js

        Firefox has the ViewSourceWith extension (among others) - maybe less
        convenient but provides a few more features :-)
        >
        <URL: https://addons.mozilla.org/firefox/394/ >
        >
        --
        Rob
        It's not a bug. The reason of that script you loaded has'nt run is it
        hasn't loaded completely.
        1. you can load the script in a script chunk before your main chunk;
        2. use XMLHttp load script from xml files, than use global function
        eval() to run it.
        3. set a setTimeout() to wait for all the code loaded completely.

        Comment

        • RobG

          #5
          Re: Dumping content of included .js


          Lich_Ray wrote:
          Firefox has the ViewSourceWith extension (among others) - maybe less
          convenient but provides a few more features :-)

          <URL: https://addons.mozilla.org/firefox/394/ >

          --
          Rob
          >
          It's not a bug. The reason of that script you loaded has'nt run is it
          hasn't loaded completely.
          1. you can load the script in a script chunk before your main chunk;
          2. use XMLHttp load script from xml files, than use global function
          eval() to run it.
          3. set a setTimeout() to wait for all the code loaded completely.
          I think you completely missed the discussion. No one mentioned bug or
          defect. There is no reason to suspect that the js file hasn't fully
          loaded. There is no benefit to using eval() to execute the code since
          the OP wants to view the source.

          Using setTimeout() presumes that load latency is an issue, which it
          isn't (see above). If it was, trying to view the source after some
          guesstimate of how long the file might take to load is not a
          particularly robust solution.

          --
          Rob

          Comment

          • Randy Webb

            #6
            Re: Dumping content of included .js

            Lich_Ray said the following on 11/8/2006 11:23 PM:
            >Firefox has the ViewSourceWith extension (among others) - maybe less
            >convenient but provides a few more features :-)
            >>
            > <URL: https://addons.mozilla.org/firefox/394/ >
            >>
            >--
            >Rob
            >
            It's not a bug. The reason of that script you loaded has'nt run is it
            hasn't loaded completely.
            Who said anything about a bug and a script that hasn't loaded completely?
            1. you can load the script in a script chunk before your main chunk;
            Yoooohooooooo, thats what is being done!
            2. use XMLHttp load script from xml files, than use global function
            eval() to run it.
            That's a dumb way to do it.
            3. set a setTimeout() to wait for all the code loaded completely.
            That won't work.


            --
            Randy
            Chance Favors The Prepared Mind
            comp.lang.javas cript FAQ - http://jibbering.com/faq
            Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

            Comment

            • Lich_Ray

              #7
              Re: Dumping content of included .js

              Sorry, I haven't view your discussion completely. I Thought that he
              want to run the script he had loaded.

              Comment

              • Dr J R Stockton

                #8
                Re: Dumping content of included .js

                In message <1163029170.816 338.64940@e3g20 00cwe.googlegro ups.com>, Wed, 8
                Nov 2006 15:39:30, nick <wassage@gmail. comwrites
                >Is it possible to dump the source code from a .js file and save it to a
                >string?
                Don't know; but I'd use it if I could.

                The source, or functional equivalent, of a function Fn [in a .js file],
                can be obtained by Fn.toString() - therefore, if you can put the whole
                needed content in the .js file as a function Fn, you can do what you
                want, more or less.

                The green-bordered boxes in
                <URL:http://www.merlyn.demo n.co.uk/js-nclds.htm get their contents via
                Fn.toString(), and the section "Code Display" shows how.


                Query : how necessary is it for an include file to be named *.js ? I
                find that to have mildly annoying consequences now that the OS
                recognises .js as an executable extension.

                It's a good idea to read the newsgroup and its FAQ. See below.

                --
                (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
                <URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Evertjan.

                  #9
                  Re: Dumping content of included .js

                  Dr J R Stockton wrote on 09 nov 2006 in comp.lang.javas cript:
                  Query : how necessary is it for an include file to be named *.js ? I
                  find that to have mildly annoying consequences now that the OS
                  recognises .js as an executable extension.
                  >
                  Not at all.

                  I often use:
                  <script type='text/javascript' src='myJs.asp'> </script>

                  giving marvellous opportunities for
                  serverside preprocessing and data input.

                  just likeEven better than:
                  <img src='myPicture. asp'>

                  =============== =
                  8 Nov 2006 15:39:30, nick <wassage@gmail. comwrites
                  >>Is it possible to dump the source code from a .js file and save it to
                  >>a string?
                  >
                  Don't know; but I'd use it if I could.
                  =============== ==
                  <script type='text/javascript' src='klok.js'></script>

                  <script type='text/javascript'>
                  alert( document.getEle mentsByTagName( 'script')[1].text );
                  </script>
                  =============== ======

                  This fails if I change [1] to [0], giving an empty string:

                  alert( typeof document.getEle mentsByTagName( 'script')[0].text );


                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Randy Webb

                    #10
                    Re: Dumping content of included .js

                    Dr J R Stockton said the following on 11/9/2006 6:48 AM:

                    <snip>
                    Query : how necessary is it for an include file to be named *.js ?
                    Not necessary at all. You could name them *.myOwnScriptEx tension as long
                    as the files on the server match the file name in your HTML including case.

                    --
                    Randy
                    Chance Favors The Prepared Mind
                    comp.lang.javas cript FAQ - http://jibbering.com/faq
                    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                    Comment

                    • Randy Webb

                      #11
                      Re: Dumping content of included .js

                      Evertjan. said the following on 11/9/2006 2:41 PM:
                      Dr J R Stockton wrote on 09 nov 2006 in comp.lang.javas cript:
                      >
                      >Query : how necessary is it for an include file to be named *.js ? I
                      >find that to have mildly annoying consequences now that the OS
                      >recognises .js as an executable extension.
                      >>
                      >
                      Not at all.
                      >
                      I often use:
                      <script type='text/javascript' src='myJs.asp'> </script>
                      >
                      giving marvellous opportunities for
                      serverside preprocessing and data input.
                      >
                      just likeEven better than:
                      <img src='myPicture. asp'>
                      >
                      =============== =
                      >
                      >8 Nov 2006 15:39:30, nick <wassage@gmail. comwrites
                      >>Is it possible to dump the source code from a .js file and save it to
                      >>a string?
                      >Don't know; but I'd use it if I could.
                      >
                      =============== ==
                      <script type='text/javascript' src='klok.js'></script>
                      >
                      <script type='text/javascript'>
                      alert( document.getEle mentsByTagName( 'script')[1].text );
                      </script>
                      =============== ======
                      >
                      This fails if I change [1] to [0], giving an empty string:
                      But using [1] I get the current script block. Inserting an empty script
                      block to cause the src element to 1 doesn't give the .text either in IE.

                      Reason? IE won't read the .text property as being the external file, it
                      is reading the contents of the script element itself. Adding a comment
                      to the script block so that is this:

                      <script type="text/javascript" src="test.js">
                      //This is a test
                      </script>
                      <script type="text/javascript">
                      var scripts = document.getEle mentsByTagName( 'SCRIPT');
                      alert(scripts[0].text);
                      </script>

                      The alert gives "//This is a test" in IE7 and - ironically enough - in
                      Firefox 2.0 as well which is incorrect behavior because the browser is
                      *supposed* to ignore anything in a script element with a src attribute.

                      --
                      Randy
                      Chance Favors The Prepared Mind
                      comp.lang.javas cript FAQ - http://jibbering.com/faq
                      Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                      Comment

                      • VK

                        #12
                        Re: Dumping content of included .js


                        Randy Webb wrote:
                        <script type="text/javascript" src="test.js">
                        //This is a test
                        </script>
                        <script type="text/javascript">
                        var scripts = document.getEle mentsByTagName( 'SCRIPT');
                        alert(scripts[0].text);
                        </script>
                        >
                        The alert gives "//This is a test" in IE7 and - ironically enough - in
                        Firefox 2.0 as well which is incorrect behavior because the browser is
                        *supposed* to ignore anything in a script element with a src attribute.
                        You are mixing node values and the executable code. Browser doesn't
                        suppose to ignore anything: same way as it doesn't ignore NOSCRIPT
                        blocks: all relevant nodes will be created, can be read back and/or
                        saved with the page source.

                        *Script engine* - if src attribute is supported and set - will not
                        execute nor even parse any code inside script element: this code will
                        be used as fall-back option for engines not supporting external
                        scripts. (There are not such engines anymore, but the mechanics left).

                        At the same time the content of the element is preserved and can be
                        read by DOM methods as you just did.

                        Comment

                        • Evertjan.

                          #13
                          Re: Dumping content of included .js

                          Randy Webb wrote on 09 nov 2006 in comp.lang.javas cript:
                          The alert gives "//This is a test" in IE7 and - ironically enough - in
                          Firefox 2.0 as well which is incorrect behavior because the browser is
                          *supposed* to ignore anything in a script element with a src attribute.
                          so we would have to fall back on XMLHTTP ?

                          =============== =============== =============
                          <base href = 'http://www.blah.abc/'>

                          <script type='text/javascript' src='jsFile.js' ></script>


                          <script type='text/javascript'>

                          var http = new ActiveXObject(" Msxml2.XMLHTTP" );

                          function getUrl(url) {
                          http.open("GET" ,url,false);
                          http.send();
                          return http.responseTe xt;
                          }

                          url= document.getEle mentsByTagName( 'base')[0].href +
                          document.getEle mentsByTagName( 'script')[0].src;

                          alert( getUrl(url) );

                          </script>
                          =============== =============== =============

                          --
                          Evertjan.
                          The Netherlands.
                          (Please change the x'es to dots in my emailaddress)

                          Comment

                          • Dr J R Stockton

                            #14
                            Re: Dumping content of included .js

                            In message <Tp-dnRpNkqeVPs7Y4p 2dnA@telcove.ne t>, Thu, 9 Nov 2006
                            16:52:14, Randy Webb <HikksNotAtHome @aol.comwrites
                            >Dr J R Stockton said the following on 11/9/2006 6:48 AM:
                            >
                            ><snip>
                            >
                            >Query : how necessary is it for an include file to be named *.js ?
                            >
                            >Not necessary at all. You could name them *.myOwnScriptEx tension as
                            >long as the files on the server match the file name in your HTML
                            >including case.

                            OK; I think I'll do that when I've decided on a suitable TLA and am
                            prepared for a Great Upload and have time to identify all appropriate
                            occurrences.

                            --
                            (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
                            Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links;
                            Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
                            No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                            Comment

                            Working...