473,698 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using eval for contents retrieved with XMLHttpRequest( )

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

--
Jul 23 '05
12 3443
Razzbar escribió:
So shall also be many users scratching their heads as their browsers
hang useless, because some cowboy figured that if you put your JS
functions on the server and use a synchronous call, you are guaranteed
they will come a'running ever time, and execute when you want them to,
not before.


If JS don't work, my sites fallback to the fact that they don't support
client-side scripting, then many browsers will be supported only because
of this.

Regards,

knocte

--
Jul 23 '05 #11
VK escribió:
The case occurs when retrieving the javascript code with
XMLHttpReques t(). It fails on Mozilla/Firefox and IE!


Actually XMLHTTP doesn't stay from "get XML or HTTP or whatever you'll
get in your head". It stays from "get XML data for HTML environment
(browser)". But I'm affraid that all hell broke loose already. Mr.
Knocte is getting his JavaScript this way, someone already tried to get
binary data stream. So soon I guess we'll see XMLHTTP used to download
music, pictures and "Star Wars Episode 3". So shall be it...


Actually, I think the feature should be just called "HttpReques t",
because nothing prevents me for getting anything I want with the Http
protocol using this.

Regards,

knocte

P.S.: Perhaps you should suggest to the responsible for DOM specs to
delete the accessibility to the ".responseT ext" attribute. That way
you'll be able to dictate how the technologies must be used.

--
Jul 23 '05 #12
VK
> Actually, I think the feature should be just called "HttpReques t",
because nothing prevents me for getting anything I want with
the Http protocol using this.


"Who can stop Hafiz from likening a ladybug to the Padishah"
An old Persian proverbe ;-)

<http://groups-beta.google.com/group/comp.lang.javas cript/browse_frm/thread/f6ce0a5e95d8bf3 0/ba5264709e33894 e?q=group:comp. lang.javascript +author:VK&rnum =19&hl=en#ba526 4709e33894e>

Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2433
by: vanisathish | last post by:
Hi I am running a client side javascript timer to periodically refresh the contents of some tables in the HTML page. The table values are dynmically binded from XML DOM object using the <XML tag in HTML>. The data is getting updated properly. but whenever the value is refreshed, the HTML page flickers. How do i avoid this flickering.. below is my html page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
8
2109
by: Jason Shohet | last post by:
I want to build a rules engine in a rules.xml page. I was thinking it would look like this: - Rules - rule1 if (0 < iTime <= .5) { iFee = 15; } - rule2 if (.5 < iTime <= 1) { iFee = 20; } - yada yada Now lets say in my c# code I loop thru the Rules node of rules.xml, and I want to evaluate each rule, given an iTime that the user inputed -- lets
3
3084
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are displayed. Can anybody know this issue and help me to resolve this. In past i received the response saying that i should download the image first and then parse the actual response and modify the src attribute of the
24
3424
by: Larry | last post by:
Hi there: I have seen numerous postings about eval() and its evils on this forum. However, one of our developers is using it in the following way, which seems like a great use of it. Page makes Ajax request to ASP.Net web service. Web service does some data lookup and builds a string representation of a Javascript array which is then returned to the client. In the ajax callback, call to eval on the returned string and voila, ...
7
3003
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm attempting to bind a user control I've written, "ImageBox", to a repeater. The user control takes a custom property, "ContentID", that will execute a database lookup and load an image.
1
14994
by: Pablo | last post by:
Dear all, I have a website where I need to fill its contents after retrieving some data from another system (not a DB, but you can think like it). I know the nature of the data, but the order that it is retrieved is random, and there is no way to order it. Therefore, it is giving me some pain to produce the html code. I was thinking of having the following javascript to place my html code. Here, "rightImg" is the ID of a DIV tag where I...
1
5298
by: Tarik Monem | last post by:
OK, I'm pretty sure this cannot work because I'm trying to use JavaScript (client-side) to write to an xml file (which is server-side) using XMLHttpRequest. Can I use PHP do what I'm trying to do? Here's my code: The function mySaveFunction() is called by clicking the "Update" button, after the user changes the data which is populated in the form fields, which was retrieved via XMLHttpRequest from an XML external file. I know that...
1
3189
by: wenijah | last post by:
Hi everyone! First thank you for reading this post and yes, you probably already see that kind of topic title somewhere but the problem I've got today might be different than the 100 topics I've seen so far that did not resolve my problem... Environment and problem: I have a page A with Ajax.js and a <div id="ajx">Content</div> that changes when clicking on a link. Another page, page B, gets loaded into page A and (tries) to load a...
13
3173
by: My Pet Programmer | last post by:
The way I usually set up and work with the XMLHttpRequest to execute server side functions and get results is this: var url = "someurl?params=" + params; var conn = createRequest(); // gets an XMLHttpRequest object conn.open("GET", url); conn.onreadystatechange = function () { if (conn.readyState == 4 && conn.status == 200) {
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.