When I create this line
var http = createRequestObject();
the second time in the abort_operation(), will this work from a variable scoping point of view? I'm not too sure about how JS vars work with scoping their "visibility". I initially am creating the http instance at the top of my functions and then in abort_operation() I am trying to re-create it again - is this structure correct?
Recall the problem is that when I abort an ajax operation (such as expand_search_window_section) and then try to call it again, it doesn't work. I have not tried this code below yet because I want to make sure the recreation of the http instance is in the correct spot from a var scoping point of view.
The key question is where can/should I be recreating the var http = createRequestObject()?
PHP Code:
function createRequestObject() {
if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
var xmlhttp = new XMLHttpRequest();
if (xmlhttp.overrideMimeType)
xmlhttp.overrideMimeType('text/xml');
}
else if (window.ActiveXObject) { // IE
try {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlhttp) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
return xmlhttp;
}
var http = createRequestObject();
function abort_operation()
{
http.abort();
var http = createRequestObject();
return;
}
function expand_search_window_section()
{
var url = "Ajax_ExpandSearch_ClickServer.php";
var params = "php_status=" + php_status;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function()
{
if (http.readyState == 4) //Finished loading the response
{
if (http.status == 200)
{
var response = http.responseText;
//alert(response);
}
else
{
alert("Ajax error: " + http.statusText);
}
}
}
http.send(params);
}