Dumping content of included .js

N

nick

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!
 
R

Randy Webb

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
 
R

RobG

Randy said:
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/ >
 
L

Lich_Ray

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

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

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.
 
R

RobG

Lich_Ray said:
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.
 
R

Randy Webb

Lich_Ray said the following on 11/8/2006 11:23 PM:
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.
 
L

Lich_Ray

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

Dr J R Stockton

Wed said:
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.
 
E

Evertjan.

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'>

================
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 );
 
R

Randy Webb

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

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.
 
R

Randy Webb

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'>

================
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.
 
V

VK

Randy said:
<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.
 
E

Evertjan.

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>
===========================================
 
D

Dr J R Stockton

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



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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top