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.XMLHttp Request) {
oXmlRequest = new XMLHttpRequest( );
oXmlRequest.ope n("GET", sURL, bAsync);
oXmlRequest.sen d(null);
}
/* branch for IE/Windows ActiveX version */
else if (window.ActiveX Object) {
oXmlRequest = new ActiveXObject(" Microsoft.XMLHT TP");
oXmlRequest.ope n("GET", sURL, bAsync);
oXmlRequest.sen d();
}
return oXmlRequest;
}
function EvalScriptFile( oDocument){
if (oDocument){
/* only if status shows "loaded" */
if (oDocument.read yState == 4) {
/* only if "OK" */
if (oDocument.stat us == 200) {
eval(oDocument. responseText);
}
else if (oDocument.stat us == 404) {
alert("File not found");
}
}
}
}
function LoadScriptFile( sURL){
var oScriptFileRequ est = RequestDocument (sURL,false);
EvalScriptFile( oScriptFileRequ est);
}
//this works:
eval("alert('he llo');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
--
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.XMLHttp Request) {
oXmlRequest = new XMLHttpRequest( );
oXmlRequest.ope n("GET", sURL, bAsync);
oXmlRequest.sen d(null);
}
/* branch for IE/Windows ActiveX version */
else if (window.ActiveX Object) {
oXmlRequest = new ActiveXObject(" Microsoft.XMLHT TP");
oXmlRequest.ope n("GET", sURL, bAsync);
oXmlRequest.sen d();
}
return oXmlRequest;
}
function EvalScriptFile( oDocument){
if (oDocument){
/* only if status shows "loaded" */
if (oDocument.read yState == 4) {
/* only if "OK" */
if (oDocument.stat us == 200) {
eval(oDocument. responseText);
}
else if (oDocument.stat us == 404) {
alert("File not found");
}
}
}
}
function LoadScriptFile( sURL){
var oScriptFileRequ est = RequestDocument (sURL,false);
EvalScriptFile( oScriptFileRequ est);
}
//this works:
eval("alert('he llo');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
--
Comment