...

View Full Version : Javascript return does not work



DaLi
06-18-2010, 03:02 PM
Hello,

I made a little code for my website but it doesn't work and I can't find out what the problem is.

The problem is that the line return 2; does return undefined and not 2 as expected.

I also tried to replace var xmlhttp2 = ''; for var xmlhttp2 = null; but it still not work.

Does anyone know what is incorrect?

Thanks in advance,


function get_image(url)
{
var xmlhttp2 = '';
xmlhttp2=GetXmlHttpObject()
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState == 4)
{
return 2;
}
};
xmlhttp2.open("GET", 'image_print.php?url=' + url, true);
xmlhttp2.send(null);

//return output;

}

randomuser773
06-18-2010, 05:26 PM
Hello,

I made a little code for my website but it doesn't work and I can't find out what the problem is.

The problem is that the line return 2; does return undefined and not 2 as expected.

I also tried to replace var xmlhttp2 = ''; for var xmlhttp2 = null; but it still not work.

Does anyone know what is incorrect?

Thanks in advance,


function get_image(url)
{
var xmlhttp2 = '';
xmlhttp2=GetXmlHttpObject()
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState == 4)
{
return 2;
}
};
xmlhttp2.open("GET", 'image_print.php?url=' + url, true);
xmlhttp2.send(null);

//return output;

} I'm sure that statement does work but you can't read it, at least not in the way you seem to expect.

I don't think you entirely appreciate what's happening here, perhaps you should start reading:
https://developer.mozilla.org/en/ajax/getting_started

DaLi
06-18-2010, 06:42 PM
I am requesting the function get_image to get a number from my database (via image_print.php).

That was working.. but for debug reasons I set "return 2" to be sure that was not the issue.

I hope this will explain something:



var image_number = get_image('http://www.testurl.com/'); // should be 2, but returns undefined..


Do you have any idea? I'm lost in code :-(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum