对使用XMLHttpRequest()检索的内容使用eval [英] Using eval for contents retrieved with XMLHttpRequest()

查看:108
本文介绍了对使用XMLHttpRequest()检索的内容使用eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我一直认为eval()函数非常灵活,而且b / b
很有用。如果我使用它,我可以在运行时定义函数!!


但是,我发现了一个eval()无法正常工作的情况。例如,
在调用函数时会起作用(alert(''hello'')),但不能用于定义函数的



使用

XMLHttpRequest()检索javascript代码时出现这种情况。它在Mozilla / Firefox和IE上失败了!


我将引用代码:


< script>

函数RequestDocument(sURL,bAsync){

var oXmlRequest;


if (window.XMLHttpRequest){

oXmlRequest = new XMLHttpRequest();

oXmlRequest.open(" GET",sURL,bAsync);

oXmlRequest.send(null);

}

/ *分支IE / Windows ActiveX版* /

else if(window.ActiveXObject )$

oXmlRequest.open(" GET",sURL,bAsync);

oXmlRequest.send();

}


返回oXmlRequest;

}


函数EvalScriptFile(oDocument){

if(oDocument){


/ *仅当状态显示已加载时* /

if(oDocument.readyState == 4){


/ *仅当OK时* /

if(oDocument.status == 200){

eval(oDocument.responseText);

}

else if(oDocument.status == 404){

alert(" File not found);

}

} < br $>
}

}


函数LoadScriptFile(sURL){

var oScriptFileRequest = RequestDocument(sURL, false);

EvalScriptFile(oScriptFileRequest);

}



//这个有效:

eval(" alert(''hello''); function test(){alert(''test'');};");

test ();


//但是,如果我放了一个文件(名为mylib.js并且位于同一个

// dir,就像这个文件一样Web服务器),以下内容:

// alert(''hello2'');

//函数test2(){alert(''test2'' ); };

//

LoadScriptFile(" mylib.js");

test2();

//调用第一个警报(hello2)!但是调试器说

// test2没有定义!

//会发生什么?

< / script>

有人知道为什么吗?提前致谢。


问候,


Andrew


-

解决方案

knocte写道:

你好。

我一直以为eval()函数非常灵活并且
很有用。如果我使用它,我可以在运行时定义函数!!

但是,我发现了一个eval()无法正常工作的情况。例如,它可以在调用函数时使用(alert(''hello'')),但不能用于定义函数。

检索javascript代码时出现这种情况使用
XMLHttpRequest()。它在Mozilla / Firefox和IE上失败了!

我将引用代码:

< script>
函数RequestDocument(sURL,bAsync){
var oXmlRequest;

/ *原生XMLHttpRequest对象的分支* /
if(window.XMLHttpRequest){
oXmlRequest = new XMLHttpRequest();
oXmlRequest.open (GET,sURL,bAsync);
oXmlRequest.send(null);
}
/ *用于IE / Windows ActiveX版本的分支* /
否则(窗口) .ActiveXObject){
oXmlRequest = new ActiveXObject(" Microsoft.XMLHTTP");
oXmlRequest.open(" GET",sURL,bAsync);
oXmlRequest.send(); <返回oXmlRequest;


函数EvalScriptFile(oDocument){
if(oDocument){
/ *仅当状态显示已加载时* /
if(oDocument.readyState == 4){

/ *仅当OK时* /
if(oDocument.status == 200){
eval(oDocument.responseText);
}
if if(oDocument.status == 404){
alert(找不到文件);
}
}
}

函数LoadScriptFile(sURL){
var oScriptFileRequest = RequestDocument(sURL,false);
EvalScriptFile(oScriptFileRequest);
}



//这个有效:
eval(" alert(''hello''); function test(){alert(''test'');};");
test();
//但是,如果我放了一个文件(名为mylib.js,位于与web服务器上的此文件相同的
//目录),则包含以下内容:
// alert(' 'hello2'');
// function test2(){alert(''test2''); };
//
LoadScriptFile(" mylib.js");
test2();
//调用第一个警报(hello2)!但是调试器说
// test2没有定义!
//会发生什么?
< / script>

有谁知道为什么?提前致谢。

问候,

Andrew



为什么要使用XMLHttpRequest来加载脚本?以下是

足够的工作并且不需要使用eval


loadScript(fileName)

{var scr = document .createElement(''script'');

scr.type =''text / javascript'';

scr.src = fileName;

document.getElementsByTagName(''head'')[0] .appendChild(scr);

}


-

Vladdy
http://www.klproductions.com


但是,这种动态注入或插入动态的逻辑方法在最新版本的Safari中不起作用。


Don写道:

但是,这种动态注入或插入动态javascript的逻辑方法在最新版本的Safari中不起作用。 / blockquote>


XMLHTTPRequest对象是否正常工作在Safari?

如果没有,那么这是一个有争议的问题。


-

Randy

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组周刊


Hello.

I have always thought that the eval() function was very flexible and
useful. If I use it, I can define functions at runtime!!

However, I have found a case where eval() does not work properly. It
works, for example, when invoking functions (alert(''hello'')), but not
for defining functions.

The case occurs when retrieving the javascript code with
XMLHttpRequest(). It fails on Mozilla/Firefox and IE!

I will quote the code:

<script>
function RequestDocument(sURL, bAsync) {
var oXmlRequest;

/* branch for native XMLHttpRequest object */
if (window.XMLHttpRequest) {
oXmlRequest = new XMLHttpRequest();
oXmlRequest.open("GET", sURL, bAsync);
oXmlRequest.send(null);
}
/* branch for IE/Windows ActiveX version */
else if (window.ActiveXObject) {
oXmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
oXmlRequest.open("GET", sURL, bAsync);
oXmlRequest.send();
}

return oXmlRequest;
}

function EvalScriptFile(oDocument){
if (oDocument){

/* only if status shows "loaded" */
if (oDocument.readyState == 4) {

/* only if "OK" */
if (oDocument.status == 200) {
eval(oDocument.responseText);
}
else if (oDocument.status == 404) {
alert("File not found");
}
}
}
}

function LoadScriptFile(sURL){
var oScriptFileRequest = RequestDocument(sURL,false);
EvalScriptFile(oScriptFileRequest);
}



//this works:
eval("alert(''hello'');function test(){ alert(''test''); };");
test();

//but, if I put on a file (called mylib.js and located at the same
//dir as this file on the web server), the following contents:
// alert(''hello2'');
// function test2(){ alert(''test2''); };
//
LoadScriptFile("mylib.js");
test2();
//the first alert is called (hello2)! but debugger says
//that test2 is not defined!
// what happens??
</script>
Do anybody know why?? Thanks in advance.

Regards,

Andrew

--

解决方案

knocte wrote:

Hello.

I have always thought that the eval() function was very flexible and
useful. If I use it, I can define functions at runtime!!

However, I have found a case where eval() does not work properly. It
works, for example, when invoking functions (alert(''hello'')), but not
for defining functions.

The case occurs when retrieving the javascript code with
XMLHttpRequest(). It fails on Mozilla/Firefox and IE!

I will quote the code:

<script>
function RequestDocument(sURL, bAsync) {
var oXmlRequest;

/* branch for native XMLHttpRequest object */
if (window.XMLHttpRequest) {
oXmlRequest = new XMLHttpRequest();
oXmlRequest.open("GET", sURL, bAsync);
oXmlRequest.send(null);
}
/* branch for IE/Windows ActiveX version */
else if (window.ActiveXObject) {
oXmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
oXmlRequest.open("GET", sURL, bAsync);
oXmlRequest.send();
}

return oXmlRequest;
}

function EvalScriptFile(oDocument){
if (oDocument){

/* only if status shows "loaded" */
if (oDocument.readyState == 4) {

/* only if "OK" */
if (oDocument.status == 200) {
eval(oDocument.responseText);
}
else if (oDocument.status == 404) {
alert("File not found");
}
}
}
}

function LoadScriptFile(sURL){
var oScriptFileRequest = RequestDocument(sURL,false);
EvalScriptFile(oScriptFileRequest);
}



//this works:
eval("alert(''hello'');function test(){ alert(''test''); };");
test();

//but, if I put on a file (called mylib.js and located at the same
//dir as this file on the web server), the following contents:
// alert(''hello2'');
// function test2(){ alert(''test2''); };
//
LoadScriptFile("mylib.js");
test2();
//the first alert is called (hello2)! but debugger says
//that test2 is not defined!
// what happens??
</script>
Do anybody know why?? Thanks in advance.

Regards,

Andrew


Why would you use XMLHttpRequest to load a script? Following does an
adequate job and does not require the use of eval

loadScript(fileName)
{ var scr = document.createElement(''script'');
scr.type = ''text/javascript'';
scr.src = fileName;
document.getElementsByTagName(''head'')[0].appendChild(scr);
}

--
Vladdy
http://www.klproductions.com


However, this sort of logical approach to injecting or inserting
javascript dynamically does NOT work in the latest version of Safari.


Don wrote:

However, this sort of logical approach to injecting or inserting
javascript dynamically does NOT work in the latest version of Safari.



Does the XMLHTTPRequest object "work" in Safari?
If not, then its a moot point.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly


这篇关于对使用XMLHttpRequest()检索的内容使用eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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