Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Dumping content of included .js

Reply
Thread Tools

Dumping content of included .js

 
 
nick
Guest
Posts: n/a
 
      11-08-2006
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!

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      11-09-2006
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/
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-09-2006

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

 
Reply With Quote
 
Lich_Ray
Guest
Posts: n/a
 
      11-09-2006
> 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.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      11-09-2006

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

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      11-09-2006
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/
 
Reply With Quote
 
Lich_Ray
Guest
Posts: n/a
 
      11-09-2006
Sorry, I haven't view your discussion completely. I Thought that he
want to run the script he had loaded.

 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      11-09-2006
In message < om>, 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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-09-2006
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)
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      11-09-2006
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
Dumping real signals in VCD Sajan VHDL 8 07-21-2007 07:39 AM
Dumping list of installed modules. Freddo Perl 1 12-30-2005 04:57 AM
Dumping the contents of an Integer Array.... kwaj VHDL 2 02-26-2004 09:53 PM
Data::Dumper How to quote keys while dumping kamal Perl 0 08-12-2003 05:23 AM



Advertisments