« Previous entry | Next entry » Browse > Snippets
Skip to comments (11)
cross browser XMLHttpRequest
Posted by Erik on Oct 29 2005 @ 20:19 :: 6270 unique visits
With AJAX programming being so populair these days I thought I post this.CODE: JAVASCRIPT
function createXMLHttpRequest() {
var types = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
];
for (var i = 0; i < types.length; i++) {
try {
return new ActiveXObject(types[i]);
} catch(e) {}
}
try {
return new XMLHttpRequest();
} catch(e) { }
return false; // XMLHttpRequest not supported
}
var types = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
];
for (var i = 0; i < types.length; i++) {
try {
return new ActiveXObject(types[i]);
} catch(e) {}
}
try {
return new XMLHttpRequest();
} catch(e) { }
return false; // XMLHttpRequest not supported
}
Modified the function after guest posting.
You can also use this code to add a XMLHttpRequest to IE so you can just use
new XMLHttpRequest(); as in other browsers.
CODE: JAVASCRIPT
if (!window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
var types = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
];
for (var i = 0; i < types.length; i++) {
try {
return new ActiveXObject(types[i]);
} catch(e) {}
}
return false; // XMLHttpRequest not supported
}
}
window.XMLHttpRequest = function() {
var types = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
];
for (var i = 0; i < types.length; i++) {
try {
return new ActiveXObject(types[i]);
} catch(e) {}
}
return false; // XMLHttpRequest not supported
}
}
11 comments posted so far
Add your own »
2. On Oct 30 2005 @ 00:03 Erik wrote:
lol, no I can't (this link will). But if you don't know what an XMLHttpRequest does you will probably never need this code.3. On Nov 19 2005 @ 10:51 guest wrote:
I think this version works better and is more stable:CODE: JAVASCRIPT
function createXMLHttpRequest()
{
// Create and initialise the temporary variable
var http_request = false;
// This check is for Mozilla, Safari, Opera,...
if (window.XMLHttpRequest)
{
try
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
catch (e)
{
http_request = false;
}
}
// For Internet Explorer
else
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
http_request = false;
}
}
}
return (http_request != null && http_request != false) ? http_request : false;
}
{
// Create and initialise the temporary variable
var http_request = false;
// This check is for Mozilla, Safari, Opera,...
if (window.XMLHttpRequest)
{
try
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
catch (e)
{
http_request = false;
}
}
// For Internet Explorer
else
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
http_request = false;
}
}
}
return (http_request != null && http_request != false) ? http_request : false;
}
4. On Nov 21 2005 @ 16:56 guest wrote:
hehe, the function in the startpost will also work correctly and maybe better, because return wil exit the function after it's called, so he won't reach te next line. ;)5. On Dec 13 2005 @ 17:36 guest wrote:
I think this a better one:CODE: JAVASCRIPT
if (!window.XMLHttpRequest)
{
window.XMLHttpRequest = function()
{
var types = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
];
for (var i = 0; i < types.length; i++)
{
try
{
return new ActiveXObject(types[i]);
}
catch(e) {}
}
return undefined;
}
}
{
window.XMLHttpRequest = function()
{
var types = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
];
for (var i = 0; i < types.length; i++)
{
try
{
return new ActiveXObject(types[i]);
}
catch(e) {}
}
return undefined;
}
}
6. On Dec 14 2005 @ 18:40 Erik wrote:
Looks interresting. I will do some tests with this and then probably modify the code I submitted.7. On Dec 14 2005 @ 23:09 Erik wrote:
Found some problems today. When XMLHttpRequest is not supported createXMLHttpRequest returns null. But when you compare the result to null you will get an error in IE (not in firefox).CODE: JAVASCRIPT
var req = createXMLHttpRequest();
if (req == null) { // error here
...
if (req == null) { // error here
...
I have fixed this by letting the function return false and doing the check like this:
CODE: JAVASCRIPT
var req = createXMLHttpRequest();
if (typeof req == 'boolean') { // error here
...
if (typeof req == 'boolean') { // error here
...
8. On Apr 30 2008 @ 04:42 dfg wrote:
dfgdfgdfgdfg9. On May 17 2008 @ 17:38 dfg wrote:
dfgdfg10. On Aug 09 2008 @ 07:29 priya wrote:
Anyone bought from www.belrion.com before ? heard they are a paypal world seller and are macfee secured. Appreciate some feedback from anyone ^^<a href = http://www.belrion.com/en/ffxi.htm > buy ffxi</a><br>
<a href = http://www.belrion.com/en/eq.htm> buy eq flat </a><br>
<a href = http://www.belrion.com/en/wow.htm> cheap wow gold</a><br>
<a href = http://www.belrion.com> LOTR gold</a><br>
<a href = http://www.belrion.com/en/sell.htm>buy aoc gold</a><br>
<a href = http://www.belrion.com/en/l2.htm> buy L2 adena</a><br>
<a href = http://www.belrion.com/en/gamesvr.php?cid=1&gid=3&sid=10 >buy gils</a><br>
<a href = http://www.belrion.com/en/eq.htm >cheap gold wow</a><br>
11. On Aug 27 2008 @ 04:54 guest wrote:
Top 10 best gambling roulette gambling casinos based on micro gaming platform.Best online black jack gambling casinos, play to win and enjoy your online
internet blackjack experience.
Learn how to play video poker and experience the thrill of gambling and beating online casinos.
Use a free poker reviews to calculate your winning poker odds.
Learn the art of horse racing system and play to win your punts at the races.
Dont buy movies, get downloadable movies movies and watch them for free.
Earn money and work from home, with forex trading system, trade the forex market.
Get all sorts of iphone games for your new iphone mobile telephone.
1. On Oct 29 2005 @ 21:17 Bhav wrote:
um could you explain what that means/does for us n00bs? ;)