访问外部脚本的代码 [英] access code of external scripts

查看:57
本文介绍了访问外部脚本的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正试图通过其text访问< scripttag

中包含的JavaScript代码。属性。如果代码嵌入在HTML页面的

中,这很有效。但是,当代码在外部文件中时,

" src"属性,它不再起作用。


有没有人知道是否有任何方法(干净简单

或作为解决方法)访问外部脚本的代码也是如此?我读了b $ b,如果有类似


< script type =" text / javascript" src =" ./ myScript.js">

alert(''hello world'');

< / script>

外部脚本和嵌入式脚本是连接的。所以我

假设这个串联的结果必须是可以访问的

不知何故。


任何想法?


谢谢,


Stefan

解决方案

Stefan Weber写道:





我正在尝试访问< scripttag
通过其text属性。如果代码嵌入在HTML页面的

中,这很有效。但是,当代码在外部文件中时,

" src"属性,它不再起作用了。



为什么?


有没有人知道是否有任何方法(干净简单)

或作为解决方法)也可以访问外部脚本的代码?我读了b $ b,如果有



,我甚至都不确定我知道你在追求什么。


< script type =" text / javascript" src =" ./ myScript.js">

alert(''hello world'');

< / script>



从什么时候开始?这不会将外部连接成内部的
。事实上,一个与

外部匹配的内部声明甚至不会覆盖外部。


外部脚本和嵌入式脚本是连接的。所以我

假设这个连接的结果必须是可访问的

不知何故。



与我们其他人分享这个网址,以便它可以永远被烧毁

我们的想法是错误的信息。


我真的很想知道你为什么要走这条路线或者你是什么?b
$ b。你说你想要访问在脚本标签中找到的代码

,这对我来说听起来很奇怪。如果你想访问它,请访问它。


< script>

function visited(){return''Access Granted''; }

提醒(访问());

< / script>


只是为了踢,你可以阅读内容SCRIPT *或*使用

XMLHttpRequest读取脚本的内容。


-

-Lost

删除要通过电子邮件回复的额外单词。不要给我发电子邮件。我是开玩笑的。不,我不是。




" Stefan Weber" < st ********** @ gmail.comwrote in message

news:11 ******************** **@h2g2000hsg.googlegro ups.com ...





G''day 。


< script type =" text / javascript" src =" ./ myScript.js">

alert(''hello world'');

< / script>

外部脚本和嵌入式脚本是连接的。所以我

假设这个连接的结果必须是可以访问的

不知何故。


任何想法?



是的。


快速调查HTML规范

http://www.w3.org/TR/html401/interac ... ml#edef-SCRIPT


显示:


< quote>

脚本可以在SCRIPT元素的内容中定义,也可以在

外部文件中定义。如果未设置src属性,则用户代理必须将元素的内容解释为脚本。如果src具有URI值,则用户

代理必须忽略元素的内容并通过

URI检索脚本。

< / quote>


非常清楚。 src或元素内容。不是两个。


-

Richard。


我正在尝试访问包含在< scripttag


中的JavaScript代码通过其text引用属性。如果代码嵌入在HTML页面的

中,这很有效。但是,当代码在外部文件中时,

" src"属性,它不再起作用了。



为什么?



因为document.getElementsByTagName(" script")[0] .text返回

空字符串。


是否有任何人知道是否有任何方法(干净简单

或作为解决方法)来访问外部脚本的代码也是?我读了
,如果有像
那样的东西



我甚至都不确定我知道你在追求什么。



我想将外部文件的内容读作字符串

(即它应该与执行


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


用于嵌入式脚本。


>


< script type =" text / javascript" src =" ./ myScript.js">

alert(''hello world'');

< / script>



从什么时候开始?这不会将外部内容连接成内部的b $ b。事实上,一个与

外部相匹配的内部声明甚至不会覆盖外部。



如果我能再次找到它,我会发布URL。


>

我会真的很想知道你为什么要走这条路线或者你是什么后来。
之后。哟你说你想要访问在脚本标签中找到的代码

,这对我来说听起来很奇怪。如果要访问它,请访问它。



我的意思是访问在将脚本读取为字符串的意义上。嗯,我

实际上想要访问一个不包含

JavaScript的脚本元素。它包含一个实验性脚本语言,它将由JavaScript解释为
,因此我需要使用

源代码访问该字符串。


我知道这听起来很奇怪,但这只是一个非常小的一步,因为某些东西(希望是大的)很开始。


只是为了踢,你可以阅读SCRIPT的内容



我不明白吗?你的意思是使用文本吗?

脚本元素的属性?遗憾的是,这不起作用......


*或*使用

XMLHttpRequest读取脚本的内容。



我知道,这样可行。这样做的问题是,这会向服务器发送额外的HTTP请求。如果可能的话,我想避免

那个。


顺便说一下,这只需要在IE中工作 - 以防万一/>
问题的专有解决方案。


谢谢,


Stefan


Hi,

I''m trying to access the JavaScript code contained in a <scripttag
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

解决方案

Stefan Weber wrote:

Hi,

I''m trying to access the JavaScript code contained in a <scripttag
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.

--
-Lost
Remove the extra words to reply by e-mail. Don''t e-mail me. I am
kidding. No I am not.



"Stefan Weber" <st**********@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...

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/interac...ml#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.

--
Richard.


I''m trying to access the JavaScript code contained in a <scripttag

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?

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

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.

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.

>

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

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


这篇关于访问外部脚本的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆