473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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="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!

Nov 8 '06 #1
13 1454
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/
Nov 8 '06 #2

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

Nov 9 '06 #3
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.

Nov 9 '06 #4

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

Nov 9 '06 #5
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/
Nov 9 '06 #6
Sorry, I haven't view your discussion completely. I Thought that he
want to run the script he had loaded.

Nov 9 '06 #7
In message <11*********************@e3g2000cwe.googlegroups.c om>, Wed, 8
Nov 2006 15:39:30, nick <wa*****@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.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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 9 '06 #8
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 <wa*****@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.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)
Nov 9 '06 #9
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/
Nov 9 '06 #10
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 <wa*****@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.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/
Nov 9 '06 #11
VK

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.

Nov 9 '06 #12
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)
Nov 10 '06 #13
In message <Tp********************@telcove.net>, Thu, 9 Nov 2006
16:52:14, Randy Webb <Hi************@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 *.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.
Nov 10 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: mostof | last post by:
I'm facing a problem with require_once... Instead of actually including the file i'm requiring, it just dumps it out as text... other functions are working quite well... the code look like...
6
by: Greg Brant | last post by:
Hi, how can i backup a table / entire DB and get the index's as well as the data / create table's etc cheers Greg
14
by: laurence | last post by:
I am implementing a comprehensive image-map generator utility, so have been studying W3C HTML 4.01 Specification (http://www.w3.org/TR/html4/struct/objects.html#h-13.6) on image maps (among other...
2
by: mr.kuhl | last post by:
I thought I would contribute a solution for once. We have a standard SUSE LAMP (Linux Apache Mysql PHP) server running some custom apps, of which one component is dumping reports out to excel. ...
10
by: ken | last post by:
hello, i'm writing a c program on a linux system. i'm debugging a segmentation fault but i don't want it to dump a core file because the memory footprint of the program is over 300Mb and i don't...
3
by: noleander | last post by:
My Vis C++ program takes forever to exit. Reason: it is dumping potential memory leaks. I like finding leaks once a month, but not every time I run. How do I turn mem leak dumping off? I...
4
by: Ganesh Muthuvelu | last post by:
Hi STAN, Stan: Thanks for your response to my previous post on reading a XSD file using your article in "https://blogs.msdn.com/stan_kitsis/archive/2005/08/06/448572.aspx". it works quite well...
4
by: namemattersnot | last post by:
Re, SCENARIO: User clicks a link and a javascript function creates dynamic content in <div id="content"></div> using: document.getElementById("content").innerHTML = something In the page's...
11
by: McKirahan | last post by:
I am looking for feedback on an approach to using PHP. Below is a stripped down version of a Home page: "index.php". The content of the site is displayed in the middle of the page and is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.