access code of external scripts

S

Stefan Weber

Hi,

I'm trying to access the JavaScript code contained in a <script> tag
via its "text" attribute. This works well, if the code is embedded in
the HTML page. However, when the code is in an external file with the
"src" attribute, it does not work anymore.

Does anybody have an idea if there is any way (be it clean and simple
or as a workaround) to access the code of external scripts as well? I
read, that if there is something like

<script type="text/javascript" src="./myScript.js">
alert('hello world');
</script>

the external script and the embedded script are concatenate. So I
assume that the result of this concatenation has to be accessible
somehow.

Any ideas?

Thanks,

Stefan
 
L

-Lost

Stefan said:
Hi,

I'm trying to access the JavaScript code contained in a <script> tag
via its "text" attribute. This works well, if the code is embedded in
the HTML page. However, when the code is in an external file with the
"src" attribute, it does not work anymore.
Why?

Does anybody have an idea if there is any way (be it clean and simple
or as a workaround) to access the code of external scripts as well? I
read, that if there is something like

I am not even entirely sure I know what you are after.
<script type="text/javascript" src="./myScript.js">
alert('hello world');
</script>

Since when does this work? That will not concatenate the external into
the internal. In fact, an internal declaration that matches the
external will not even overwrite the external.
the external script and the embedded script are concatenate. So I
assume that the result of this concatenation has to be accessible
somehow.

Share the URL with the rest of us so that it can forever be burned in
our minds as misinformation.

I would really like to know why you are going this route or what you are
after. You say you want to "access" the code found within a script tag
and that just sounds weird to me. If you want to access it, access it.

<script>
function accessed() { return 'Access Granted'; }
alert(accessed());
</script>

Just for kicks, you could read the contents of SCRIPT *or* use
XMLHttpRequest to read the script's contents.
 
R

rf

Stefan Weber said:
Hi,
G'day.

<script type="text/javascript" src="./myScript.js">
alert('hello world');
</script>

the external script and the embedded script are concatenate. So I
assume that the result of this concatenation has to be accessible
somehow.

Any ideas?

Yes.

A quick investigation of the HTML specifications

http://www.w3.org/TR/html401/interact/scripts.html#edef-SCRIPT

reveals:

<quote>
The script may be defined within the contents of the SCRIPT element or in an
external file. If the src attribute is not set, user agents must interpret
the contents of the element as the script. If the src has a URI value, user
agents must ignore the element's contents and retrieve the script via the
URI.
</quote>

Pretty clear. src or element content. Not both.
 
S

Stefan Weber

I'm trying to access the JavaScript code contained in a said:

Because document.getElementsByTagName("script")[0].text returns the
empty string.
I am not even entirely sure I know what you are after.

I would like to read the contents of the external file as a string
(i.e. it should have the same effect as doing

document.getElementsByTagName("script")[0].text

for an embedded script.
Since when does this work? That will not concatenate the external into
the internal. In fact, an internal declaration that matches the
external will not even overwrite the external.

I will post the URL if I can find it again.
I would really like to know why you are going this route or what you are
after. You say you want to "access" the code found within a script tag
and that just sounds weird to me. If you want to access it, access it.

I mean "access" in the sense of reading the script as string. Hmm, I
actually want to access a script element that does not contain
JavaScript. It contains an experimental scripting language that would
be interpreted by JavaScript, thus I need to access the string with
the source code.

I know this sounds weird, but it is just a very tiny step at the
beginning of something (hopefully) big :)
Just for kicks, you could read the contents of SCRIPT

I don't understand that? Do you mean using the "text" attribute of the
script element? This unfortunately does not work...
*or* use
XMLHttpRequest to read the script's contents.

I know, this would work. The problem with this is that this would send
an additional HTTP Request to the server. If possible, I want to avoid
that.

By the way, this only has to work in IE -- just in case ther is a
proprietary solution to the problem.

Thanks,

Stefan
 
S

Stefan Weber

A quick investigation of the HTML specifications

http://www.w3.org/TR/html401/interact/scripts.html#edef-SCRIPT

reveals:

<quote>
The script may be defined within the contents of the SCRIPT element or in an
external file. If the src attribute is not set, user agents must interpret
the contents of the element as the script. If the src has a URI value, user
agents must ignore the element's contents and retrieve the script via the
URI.
</quote>

Pretty clear. src or element content. Not both.

Thank you :) But this is not the actual problem. I only want to access
the source code (as a string) from the external file.
 
L

-Lost

Stefan said:

Because document.getElementsByTagName("script")[0].text returns the
empty string.

Ah, so. Well too bad you said it only has to work for Internet
Explorer. Opera, like the obsequious lapdog it is, lovingly offers you
the contents of an external script with that line of code.

Of course, any real browser behaves as expected. Surprisingly so does
Internet Explorer.
I am not even entirely sure I know what you are after.

I would like to read the contents of the external file as a string
(i.e. it should have the same effect as doing

document.getElementsByTagName("script")[0].text

See above about Opera.
for an embedded script.

I will post the URL if I can find it again.

Please do. I will bookmark it as a source of misinformation. ;)
I mean "access" in the sense of reading the script as string. Hmm, I
actually want to access a script element that does not contain
JavaScript. It contains an experimental scripting language that would
be interpreted by JavaScript, thus I need to access the string with
the source code.

I know this sounds weird, but it is just a very tiny step at the
beginning of something (hopefully) big :)
Gotcha.


I don't understand that? Do you mean using the "text" attribute of the
script element? This unfortunately does not work...

I meant exactly what you already knew.

document.getElementsByTagName('script')[0].text and .innerHTML
I know, this would work. The problem with this is that this would send
an additional HTTP Request to the server. If possible, I want to avoid
that.

Why? (I am just being nosy now.)

Anyway, you wish to eliminate an additional HTTP request? Then don't
even load the script. Use an XHR to grab it and process it as you see fit.
By the way, this only has to work in IE -- just in case ther is a
proprietary solution to the problem.

Is this for an intranet application? If not, shame on you.
 
M

Martin Honnen

Stefan said:
But this is not the actual problem. I only want to access
the source code (as a string) from the external file.

You can try to read it, for instance using XMLHTTP and its responseText
property.
 
S

Stefan Weber

*or* use
Why? (I am just being nosy now.)

Because "my" scripting language should be processed as naturally as
possible, i.e. if possible in the same way as JavaScript.
Anyway, you wish to eliminate an additional HTTP request? Then don't
even load the script. Use an XHR to grab it and process it as you see fit.

Hmm, don't get you here. The browser automatically does the request
anyway. So the XHR would be the second request for the same source
anyway (at least this would probably use the cached version). How do
you suggest to do it with only a single request per script?
Is this for an intranet application? If not, shame on you.

I'd call it a research prototype. I know it's not nice, but if we get
this working somehow, I see a chance that browsers will support this
kind of scripting language in the future more naturally.
 
S

Stefan Weber

You can try to read it, for instance using XMLHTTP and its responseText
property.

Thanks, I'll do it like that if I can't find another possibility. As
mentioned in another post, this would obviously result in having two
HTTP requests for every external script--I would like to avoid.
 
M

Martin Honnen

Stefan said:
Thanks, I'll do it like that if I can't find another possibility. As
mentioned in another post, this would obviously result in having two
HTTP requests for every external script--I would like to avoid.

In many cases you can expect the browser to fetch the script file from
its cache on the second request.
 
L

-Lost

Stefan said:
Because "my" scripting language should be processed as naturally as
possible, i.e. if possible in the same way as JavaScript.


Hmm, don't get you here. The browser automatically does the request
anyway. So the XHR would be the second request for the same source
anyway (at least this would probably use the cached version). How do
you suggest to do it with only a single request per script?

Like Mr. Honnen said in another post. But more specifically I meant,
don't load your script via a SCRIPT tag.

Use an XHR to do it, then parse it when you get it. Thereby, only one
request.
I'd call it a research prototype. I know it's not nice, but if we get
this working somehow, I see a chance that browsers will support this
kind of scripting language in the future more naturally.

I am not sure I follow. You plan on reinventing JavaScript?
 

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
474,160
Messages
2,570,889
Members
47,423
Latest member
henerygril

Latest Threads

Top