Dumping content of included .js

Discussion in 'Javascript' started by nick, Nov 8, 2006.

  1. nick

    nick Guest

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

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

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

    Thanks in advance!
     
    nick, Nov 8, 2006
    #1
    1. Advertising

  2. nick

    Randy Webb Guest

    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.scripts[0].text

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Nov 9, 2006
    #2
    1. Advertising

  3. nick

    RobG Guest

    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.scripts[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
     
    RobG, Nov 9, 2006
    #3
  4. nick

    Lich_Ray Guest

    > 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.
     
    Lich_Ray, Nov 9, 2006
    #4
  5. nick

    RobG Guest

    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
     
    RobG, Nov 9, 2006
    #5
  6. nick

    Randy Webb Guest

    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.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Nov 9, 2006
    #6
  7. nick

    Lich_Ray Guest

    Sorry, I haven't view your discussion completely. I Thought that he
    want to run the script he had loaded.
     
    Lich_Ray, Nov 9, 2006
    #7
  8. In message <>, Wed, 8
    Nov 2006 15:39:30, nick <> writes
    >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.demon.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.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Nov 9, 2006
    #8
  9. nick

    Evertjan. Guest

    Dr J R Stockton wrote on 09 nov 2006 in comp.lang.javascript:

    > 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 <> writes
    >>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.getElementsByTagName('script')[1].text );
    </script>
    =====================

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

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


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 9, 2006
    #9
  10. nick

    Randy Webb Guest

    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 *.myOwnScriptExtension 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.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Nov 9, 2006
    #10
  11. nick

    Randy Webb Guest

    Evertjan. said the following on 11/9/2006 2:41 PM:
    > Dr J R Stockton wrote on 09 nov 2006 in comp.lang.javascript:
    >
    >> 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 <> writes
    >>> 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.getElementsByTagName('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.getElementsByTagName('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.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Nov 9, 2006
    #11
  12. nick

    VK Guest

    Randy Webb wrote:
    > <script type="text/javascript" src="test.js">
    > //This is a test
    > </script>
    > <script type="text/javascript">
    > var scripts = document.getElementsByTagName('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.
     
    VK, Nov 9, 2006
    #12
  13. nick

    Evertjan. Guest

    Randy Webb wrote on 09 nov 2006 in comp.lang.javascript:

    > 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.responseText;
    }

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

    alert( getUrl(url) );

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

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 10, 2006
    #13
  14. In message <>, Thu, 9 Nov 2006
    16:52:14, Randy Webb <> writes
    >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 *.myOwnScriptExtension 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.demon.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.
     
    Dr J R Stockton, Nov 10, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sajan

    Dumping real signals in VCD

    Sajan, Sep 19, 2003, in forum: VHDL
    Replies:
    8
    Views:
    7,260
    vlsi.engg27
    Jul 21, 2007
  2. kwaj
    Replies:
    2
    Views:
    488
  3. kamal
    Replies:
    0
    Views:
    1,006
    kamal
    Aug 12, 2003
  4. Freddo
    Replies:
    1
    Views:
    2,130
    Eric J. Roode
    Dec 30, 2005
  5. hazz
    Replies:
    6
    Views:
    50,027
    SkyUCHC
    Jun 9, 2010
Loading...

Share This Page