i thought i know what I was doing - calling a function from a string

W

windandwaves

Hi Folk

Consider the following function:

function UpdateHtml(url, parameters, onready) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
http_request.overrideMimeType('text/xml');
//http_request.overrideMimeType('text/html');
}
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert('could not load data');
}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
//var onreadyfunction = new function() { eval(onready);}
http_request.onreadystatechange = updatemap;
var geturl = url + '?' + parameters;
document.getElementById(idname + 'title').innerHTML = "loading new map
.... " + geturl;
http_request.open('GET', geturl, true);
http_request.async = false;
http_request.send(null);
}

It opens xml and assigns a function that should run when it is loaded.


In the form above, it works, however, I want it to work with the
variable that I pass. When I rewrite the JS below from:
//var onreadyfunction = new function() { eval(onready);}
http_request.onreadystatechange = updatemap;
to
var onreadyfunction = new function() { eval(onready);}
http_request.onreadystatechange = onreadyfunction;
it does not work

what am I doing wrong?

Thanks heaps in advance

Nicolaas
 
P

pcx99

windandwaves said:
In the form above, it works, however, I want it to work with the
variable that I pass. When I rewrite the JS below from:
//var onreadyfunction = new function() { eval(onready);}
http_request.onreadystatechange = updatemap;
to
var onreadyfunction = new function() { eval(onready);}
http_request.onreadystatechange = onreadyfunction;
it does not work

what am I doing wrong?

http_request.onreadystatechange = function() { eval(onready); }


Ditch the new. Ditch trying to use a variable to hold the function. I
would also note that doing a JSON eval in this context is pretty
dangerous since you're not doing a check of the readystate nor the
status of the data. Of course there's no surprise there. I'm not a
big fan of JSON and eval but to each his own.

G'luck!
 
W

windandwaves

pcx99 said:
http_request.onreadystatechange = function() { eval(onready); }


Ditch the new. Ditch trying to use a variable to hold the function. I
would also note that doing a JSON eval in this context is pretty
dangerous since you're not doing a check of the readystate nor the
status of the data. Of course there's no surprise there. I'm not a
big fan of JSON and eval but to each his own.


Thanks a million. It is dangerous, but it works - for now :)

Thanks
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top