-
Jul 4th, 2008, 03:22 PM
#1
Thread Starter
Frenzied Member
Javascript function return result
Hi all,
I'm trying to make a wrapper for the XMLHttp object (ajax). Here's my function to make an ajax call:
Code:
function ajaxCall(url)
{
xmlHttp = getXmlHttObj();
xmlHttp.open(url)
}
//usage
var HTML = ajaxCall(...);
//HTML of the page called is now in HTML var
Obviously this won't work because my ajaxCall function does not return a value. But that's what I want.
When firing open() as you know the responseText is returned in the stateChanged function, but how can I get it back to my original function, and return it into the HTML var?
Is it possible?
If not, can I pass a function as an argument to a function such as:
Code:
function ajaxCall(url, processHTML)
{
//call ajax, when got data - pass it to the processHTML callback
}
function processHTML(data)
{
//got the data returned, in data var
}
Cheers
-
Jul 4th, 2008, 09:21 PM
#2
Re: Javascript function return result
This maybe a stupid answer but try:
Code:
return ('put code here);
-
Jul 5th, 2008, 02:41 AM
#3
Re: Javascript function return result
You can have an inline function as your XmlHttpRequest callback method in which you set the value of a variable. You can also (I think) pass the method name that you want to be the callback method, so this callback method can be the one that receives the value of the HTML after which you continue your processing.
-
Jul 5th, 2008, 08:55 AM
#4
Thread Starter
Frenzied Member
Re: Javascript function return result
Originally Posted by mendhak
You can have an inline function as your XmlHttpRequest callback method in which you set the value of a variable. You can also (I think) pass the method name that you want to be the callback method, so this callback method can be the one that receives the value of the HTML after which you continue your processing.
How can this be done?
-
Jul 5th, 2008, 09:41 AM
#5
Re: Javascript function return result
Code:
xmlHttp.onreadystatechange = SetValue;
function SetValue()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
HTMLString = xmlHttp.responseBody;
}
}
HTMLString was declared outside the functions at 'page' level.
Another way to do this would be, in the called function,
Code:
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
if (xmlHttp.responseText)
{
return request.responseText;
}
}
};
-
Jul 6th, 2008, 11:47 AM
#6
Thread Starter
Frenzied Member
Re: Javascript function return result
Ok, I tried your second example, I didn't work, maybe I'm missing something, what was the 'request' obj? I changed it to xmlHttp.
Then responseText gets returned to the ajaxCall function, but I then need to somehow pickup that data and return it from ajaxCall to whatever made the call.
The only way I could get it working was a dirty way:
Code:
function callAjax()
{
var xmlHttp = GetXmlHttpObject();
var output;
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return false;
}
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
if (xmlHttp.responseText)
{
output = xmlHttp.responseText;
}
}
};
xmlHttp.open("GET",'http://www.google.co.uk',true);
xmlHttp.send(null);
while(!output)
{
// wait
}
return output;
}
alert( callAjax() );
Obviously this is unacceptable.
How can the responseText be returned to ajaxCall, and then returned to whatever called it?
-
Jul 10th, 2008, 01:52 AM
#7
Re: Javascript function return result
Then instead of return xmlHttp.responseText you can call MethodName(xmlHttp.responseText).
-
Jul 10th, 2008, 10:39 PM
#8
Fanatic Member
Re: Javascript function return result
Hmm. I have the same problem. I need it like this
Code:
<input type="button" name="Submit32" value="Check" onClick="document.wplPart.fromPartName.value = getPartName(document.wplPart.fromPartNo.value)">
Having a script like
Code:
function getPartName(partNo) {
var xhr;
try {
xhr = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e2) {
try {
xhr = new XMLHttpRequest();
} catch (e3) {
xhr = false;
}
}
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// I need to return the value from here.
}
}
};
xhr.open("GET", "index.php?m=item&a=getPartName&partNo=" + partNo, true);
xhr.send(null);
}
From status 200 I need to return it so that I can use it like someTag.value = getPartName(someOtherTag.value). I can't seem to figure out how to do this. Any help? Thanks!
-
Jul 11th, 2008, 03:43 AM
#9
Re: Javascript function return result
Guys, it's a callback. You can specify a method. The method can perform your document writes or get return values, etc.
So in nebulom's example, instead of
document.wplPart.fromPartName.value = getPartName(document.wplPart.fromPartNo.value)
to
WritePartNameToTheInputField(document.wplPart.fromParNo.value, theInputFIeld);
-
Jul 11th, 2008, 07:14 AM
#10
Fanatic Member
Re: Javascript function return result
Thanks mend, I'm doing it right now.
-
Jul 11th, 2008, 09:46 AM
#11
Frenzied Member
Re: Javascript function return result
It's really easy to write Object Oriented Javascript
javascript Code:
function DoSomethingWithResult(result)
{
// result = responseText
}
function Ajax
{
this.GetXmlHttpRequest =
function()
{
// no browser compatibility
return new XMLHttpRequest();
}
this.Request =
function(url, callback)
{
var xmlHttp = this.GetXmlHttpRequest();
xmlHttp.onreadystatechange =
function()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
// call your function!
callback(xmlHttp.responseText);
}
}
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
}
Then the call is simple:
javascript Code:
var ajax = new Ajax();
ajax.Request("page.php?param1=this¶m2=that", DoSomethingWithResult);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|