|
|
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. | |
| |
| |
| |
| |
| |
|
|
There is no answer at this time. |
|
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 :) |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |