E
eholz1
Hello JavaScript group,
I am trying to use/learn something about using AJAX (aka JavaScript)
for web pages, etc.
I am trying to use code out of "Ajax Web Applications" by Matthew
Eernisse.
This is just a simple affair using two html pages (ajaxtest.html and
ajaxmonitor.html).
It the case of ajaxtest.html all it does is flash a window.alert on
the screen with the word "OK".
The ajaxmonitor.html polls the server and has a little timing loop.
Both of these pages work in Firefox and Safari. Needless to say IE 6
and 7 i see no results. This is on several different computers (PCs).
I have searched around, but have had no luck it getting this code to
play on IE.
I am sure I am missing something. I have an external java script file
(ajax.js) which is supposed to create an XMLHttpRequestObject class,
with some methods, properties, etc. The html page merely instantiates
the "object" and accesses methods, etc.
I will attach my ajax.js file and the ajaxtest.html code. Would you
please be so kind as to point me in the right direction so this will
work in IE as well????
Code:
*****************************************************************************
ajax.js
function Ajax() {
this.req = null;
this.url = null;
this.method = 'GET';
this.async = true;
this.status = null;
this.statusText = '';
this.postData = null;
this.readyState = null;
this.responseText = null;
this.responseXML = null;
this.handleResp = null;
this.responseFormat = 'text'; //'text', 'xml', or 'object'
this.mimeType = null;
//test function for script...
this.sayHello = function() {
this.message = 'Hello Tester!';
window.alert(this.message);
};
//init method
this.init = function() {
if (!this.req) {
try {
//try to create object for Firefox, Safari, IE7, etc.
this.req = new XMLHttpRequest();
}
catch (e) {
try {
//try to create object for later verisons of IE.
//this.req = new ActiveXObject('MSXML2.XMLHTTP');
this.req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (e) {
try {
//try to create object for later verisons of IE.
this.req = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
//could not create an HTTPRequest Object
window.alert("cannot create object");
return false;
}
}
}
}
return this.req;
};
/* good this.init = function() {
var i = 0;
var reqTry = [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject('Msxml2.XMLHTTP') },
function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];
while (!this.req && (i < reqTry.length)) {
try {
this.req = reqTry[i++]();
}
catch(e) {}
}
return true;
};*/
// send a request method
this.doReq = function() {
if (!this.init()) {
alert('Could not create XMLHttpRequest object.');
return;
}
this.req.open(this.method, this.url, this.async);
if (this.mimeType) {
try {
req.overrideMimeType(this.mimeType);
}
catch (e) {
// could not override mime type -- IE 6 or Opera
}
}
var self = this; //fixes the loss-of-scope in inner function
this.req.onreadystatechange = function() {
var resp = null;
if (self.req.readyState == 4) {
//Do Stuff Here
switch (self.responseFormat) {
case 'text':
resp = self.req.responseText;
break;
case 'xml':
resp = self.responseXML;
break;
case 'object':
resp = req;
break;
}
if (self.req.status >= 200 && self.req.status <= 299) {
self.handleResp(resp);
}
else {
self.handleErr(resp);
}
}
};
this.req.send(this.postData);
};
// set mime type method
this.setMimeType = function(mimeType) {
this.mimeType = mimeType;
};
// error handler
this.handleErr = function() {
var errorWin;
try {
errorWin = window.open('', 'errorWin');
errorWin.document.body.innerHTML = this.responseText;
}
catch (e) {
alert('An error occurred, but the error message cannot be '
+ 'displayed. This is probably because of your browser\'s '
+ 'pop-up blocker.\n'
+ '\n'
+ 'Status Code: ' + this.req.status + '\n'
+ 'Status Description: ' + this.req.statusText);
}
};
// set error handle methods
this.setHandlerErr = function(funcRef) {
this.handleErr = funcRef;
};
this.setHandlerBoth = function(funcRef) {
this.handleResp = funcRef;
this.handleErr = funcRef;
};
this.abort = function() {
if (this.req) {
this.req.onreadystatechange = function() { };
this.req.abort();
this.req = null;
}
};
// the GET method...
this.doGet = function(url, hand, format) {
this.url = url;
this.handleResp = hand;
this.responseFormat = format || 'text';
this.doReq();
};
} //end ajax class??
*****************************************************HTML
CODE***************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/1999/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax Test Page</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript" src="javascripts/ajax.js"</script>
<script type="text/javascript">
var hand = function(str) {
alert('from the hand func: ' + str);
}
var mie = new Ajax();
//mie.setMimeType('text/xml');
mie.doGet('fakeserver.php', hand);
//mie.sayHello();
//window.alert("hi there");
</script>
</head>
<body>
</body>
</html>
Thank you for any help,
eholz1
I am trying to use/learn something about using AJAX (aka JavaScript)
for web pages, etc.
I am trying to use code out of "Ajax Web Applications" by Matthew
Eernisse.
This is just a simple affair using two html pages (ajaxtest.html and
ajaxmonitor.html).
It the case of ajaxtest.html all it does is flash a window.alert on
the screen with the word "OK".
The ajaxmonitor.html polls the server and has a little timing loop.
Both of these pages work in Firefox and Safari. Needless to say IE 6
and 7 i see no results. This is on several different computers (PCs).
I have searched around, but have had no luck it getting this code to
play on IE.
I am sure I am missing something. I have an external java script file
(ajax.js) which is supposed to create an XMLHttpRequestObject class,
with some methods, properties, etc. The html page merely instantiates
the "object" and accesses methods, etc.
I will attach my ajax.js file and the ajaxtest.html code. Would you
please be so kind as to point me in the right direction so this will
work in IE as well????
Code:
*****************************************************************************
ajax.js
function Ajax() {
this.req = null;
this.url = null;
this.method = 'GET';
this.async = true;
this.status = null;
this.statusText = '';
this.postData = null;
this.readyState = null;
this.responseText = null;
this.responseXML = null;
this.handleResp = null;
this.responseFormat = 'text'; //'text', 'xml', or 'object'
this.mimeType = null;
//test function for script...
this.sayHello = function() {
this.message = 'Hello Tester!';
window.alert(this.message);
};
//init method
this.init = function() {
if (!this.req) {
try {
//try to create object for Firefox, Safari, IE7, etc.
this.req = new XMLHttpRequest();
}
catch (e) {
try {
//try to create object for later verisons of IE.
//this.req = new ActiveXObject('MSXML2.XMLHTTP');
this.req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (e) {
try {
//try to create object for later verisons of IE.
this.req = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
//could not create an HTTPRequest Object
window.alert("cannot create object");
return false;
}
}
}
}
return this.req;
};
/* good this.init = function() {
var i = 0;
var reqTry = [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject('Msxml2.XMLHTTP') },
function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];
while (!this.req && (i < reqTry.length)) {
try {
this.req = reqTry[i++]();
}
catch(e) {}
}
return true;
};*/
// send a request method
this.doReq = function() {
if (!this.init()) {
alert('Could not create XMLHttpRequest object.');
return;
}
this.req.open(this.method, this.url, this.async);
if (this.mimeType) {
try {
req.overrideMimeType(this.mimeType);
}
catch (e) {
// could not override mime type -- IE 6 or Opera
}
}
var self = this; //fixes the loss-of-scope in inner function
this.req.onreadystatechange = function() {
var resp = null;
if (self.req.readyState == 4) {
//Do Stuff Here
switch (self.responseFormat) {
case 'text':
resp = self.req.responseText;
break;
case 'xml':
resp = self.responseXML;
break;
case 'object':
resp = req;
break;
}
if (self.req.status >= 200 && self.req.status <= 299) {
self.handleResp(resp);
}
else {
self.handleErr(resp);
}
}
};
this.req.send(this.postData);
};
// set mime type method
this.setMimeType = function(mimeType) {
this.mimeType = mimeType;
};
// error handler
this.handleErr = function() {
var errorWin;
try {
errorWin = window.open('', 'errorWin');
errorWin.document.body.innerHTML = this.responseText;
}
catch (e) {
alert('An error occurred, but the error message cannot be '
+ 'displayed. This is probably because of your browser\'s '
+ 'pop-up blocker.\n'
+ '\n'
+ 'Status Code: ' + this.req.status + '\n'
+ 'Status Description: ' + this.req.statusText);
}
};
// set error handle methods
this.setHandlerErr = function(funcRef) {
this.handleErr = funcRef;
};
this.setHandlerBoth = function(funcRef) {
this.handleResp = funcRef;
this.handleErr = funcRef;
};
this.abort = function() {
if (this.req) {
this.req.onreadystatechange = function() { };
this.req.abort();
this.req = null;
}
};
// the GET method...
this.doGet = function(url, hand, format) {
this.url = url;
this.handleResp = hand;
this.responseFormat = format || 'text';
this.doReq();
};
} //end ajax class??
*****************************************************HTML
CODE***************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/1999/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax Test Page</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript" src="javascripts/ajax.js"</script>
<script type="text/javascript">
var hand = function(str) {
alert('from the hand func: ' + str);
}
var mie = new Ajax();
//mie.setMimeType('text/xml');
mie.doGet('fakeserver.php', hand);
//mie.sayHello();
//window.alert("hi there");
</script>
</head>
<body>
</body>
</html>
Thank you for any help,
eholz1