Google Answers Logo
View Question
 
Q: javascript: display source of external script ( No Answer,   7 Comments )
Question  
Subject: javascript: display source of external script
Category: Computers > Programming
Asked by: emmettbrown-ga
List Price: $100.00
Posted: 24 Oct 2005 13:24 PDT
Expires: 23 Nov 2005 12:24 PST
Question ID: 584332
I have a <script> element that defines an external script:
<script src="external.src">

I want to display the contents of that script.

---

I already know how to do this if the script is inline (as opposed to
external - as defined by the 'src' attribute). Here is one way this
can be accomplished:

// this <script> element contains the source I want to display
<script id="jsblock">...content...</script>

// and this displays it
<script>alert(jsblock.innerHTML);</script>

This will result in the javascript alert "...content..."

Applying this technique to an external script does not work, as
innerHTML returns "" in that case.

---

To summarize, I would like to display the content of an external
script. The answer to this question should be in the form of
functioning javascript code.
This code must work in IE6. Preferably, it will work in most other
browsers as well.

Feel free to ask for clarification if required.

Request for Question Clarification by hammer-ga on 24 Oct 2005 14:36 PDT
Must it be displayed in an alert, or can it be shown via some other means?

- Hammer

Clarification of Question by emmettbrown-ga on 24 Oct 2005 15:31 PDT
It does not need to be an alert. It is fine to print the javascript to
the page, or to display it by any other means that you think of, where
the solution is written in javascript.

Clarification of Question by emmettbrown-ga on 24 Oct 2005 21:30 PDT
Thanks willcodeforfood.

That was my original idea. Unfortunately, you can't access a file that
is not on the same domain this way. If there actually is some way to
accomplish this using Msxml2 please let me know -- that would be an
acceptable solution.

Request for Question Clarification by hammer-ga on 25 Oct 2005 04:44 PDT
What happens if you simply browse to the .js file?

http://www.your_domain.com/scripts/your_script.js

- Hammer

Request for Question Clarification by hammer-ga on 25 Oct 2005 04:45 PDT
BTW, the above URL is an example, not a link to an actual script.

- Hammer

Clarification of Question by emmettbrown-ga on 25 Oct 2005 10:23 PDT
Thanks Hammer,

Of course, one can view the source manually. That is trivial.

What I want to accomplish is to have the html page that loads the
script, also display it. Clearly, the browser reads in that javascript
code (as it executes it, after all). Now, I just want to display it as
well.
Answer  
There is no answer at this time.

Comments  
Subject: Re: javascript: display source of external script
From: willcodeforfood-ga on 24 Oct 2005 17:11 PDT
 
This is kind of a workaround and may not help in your situation, but
it may get you on to the right track or help you find something
better:

<html>
<head>
	<script src="external.src"></script>
	<script>
	function test()
	{
		var srcfile = document.body.parentNode.childNodes[0].childNodes[1].src
		srcfile = document.location.href + "/../" + srcfile
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		xmlhttp.Open("GET", srcfile, false); 
		xmlhttp.Send()
		alert(xmlhttp.responseText)

	}
	</script>
</head>
<body onLoad="test()">
	<span>Hello</span>
	
</body>
</html>
Subject: Re: javascript: display source of external script
From: willcodeforfood-ga on 25 Oct 2005 10:27 PDT
 
emmettbrown,

That's a tough little wrinkle you've thrown in.  Your choices, it
seems to me, are to either just display the javascript src for the
user or you've got to make an activex plugin for IE and go outside the
sandbox.  If you want to roll your own plugin component, you're in for
some late nights.  On the other hand, simply displaying the external
source file is a cinch:

<html>
<head>
	<script src="external.src"></script>
	<script>
	function test()
	{
		var srcfile = document.body.parentNode.childNodes[0].childNodes[1].src
		srcfile = document.location.href + "/../" + srcfile
		// xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		// xmlhttp.Open("GET", srcfile, false); 
		// xmlhttp.Send()
		// alert(xmlhttp.responseText)
		document.getElementById("extsrc").src = srcfile

	}
	</script>
</head>
<body onLoad="test()">
	<span>Hello</span>
	<br>
	<iframe id="extsrc" style="height:200px; width:400px"></iframe>
	
</body>
</html>
Subject: Re: javascript: display source of external script
From: emmettbrown-ga on 25 Oct 2005 12:32 PDT
 
Thanks willcodeforfood,

That's close. I'd like to do this without using a frame. Is there some
other element type that could display the javascript code in the same
way as the iframe accomplishes this?

ActiveX isn't an option for this project.
Subject: Re: javascript: display source of external script
From: xenru-ga on 26 Oct 2005 04:36 PDT
 
This is my example for this problem. But It will work under any browser:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script src="http://some/script.js" type="text/javascript"
language="javascript" id="showme">
//script
</script>

<script type="text/javascript" language="javascript">
    var http_request = false;
    function makeRequest(url) {
        http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, true);
        http_request.send(null);
    }
    function alertContents() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                // show contents of downloaded file
                alert(http_request.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
</script>
</head>

<body>

<script>
 // Geting element by id
 doc = document.getElementById('showme')
 // This attribure return src for element
 alert(doc.src)
 // Sending request, when browser download file it will be open in
alert window by alertContents function
 makeRequest(doc.src)
</script>

</body>
</html>
Subject: Re: javascript: display source of external script
From: emmettbrown-ga on 26 Oct 2005 10:40 PDT
 
xenru,

Thanks for the well-written code. 

This was my first attempt at solving the problem as well.
Unfortunately, it only works if the external script is on the same
domain as the page calling it. I need a solution that works whether
the external script is on the same domain or not.
Subject: Re: javascript: display source of external script
From: xenru-ga on 26 Oct 2005 14:15 PDT
 
emmettbrown,

Sorry, but XMLHttpRequest() have some limitations. You can call for
pages only on the same domain :(

But, TMTOWTDI! ;)

We can resolve this problem on server side asking pages through local
proxy. (I'm using python scripting language for this example):

===========================================================
#!/usr/local/bin/python
# I'm python script, plz
# place me in /cgi-bin/fetcher.py
import sys
import cgi
import urllib2

form = cgi.FieldStorage()           
data = getform(['url'],form)

print "Content-type: text/html"
print
from urllib import urlopen
page = urlopen(url).read()
print page
===========================================================


And change your JavaScript code:

===========================================================
 alert(doc.src)
 // Sending request, when browser download file it
 // will be open in alert window by alertContents function
 makeRequest('/cgi-bin/fetcher.py?url='+doc.src)
</script>

===========================================================
Subject: Re: javascript: display source of external script
From: jamal_s-ga on 06 Nov 2005 07:13 PST
 
Hello,

Why not use PHP and Javascript together, then you can all the external
files you want :)

If you really want external files by using Javascript, then you must
change the priviliges in the browser you are using, and I dont think
this is a good idea.

Regards,
friend :)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy