giovedì, novembre 02, 2006

Final method to obtain a dom Document ?

after the reading of this post on IE Blog talking about using the right version of msxml I come of a possible solution to get the XMLHttpRequest:

function getXDOC(){
//W3C native Object
if (window.XMLHttpRequest)return new XMLHttpRequest();

//IE 4.0 fallback [try..catch]
var progIDs=[];
progIDs[0]='Msxml2.XMLHTTP.3.0';
var i = 0;
/*@cc_on
progIDs = [ 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0']; //6.0 is better of 3.0
for (; i < progIDs.length; i++) {
try{
@*/
if (window.ActiveXObject){
var xmlDOM = new ActiveXObject(progIDs[i]);
xmlDOM.setProperty("SelectionLanguage", "XPath");//because 3.0 default is XSLPattern
return xmlDOM;
}
/*@cc_on
}
catch (ex) {
}
}
@*/
return null;
}


Enjoy ;)

Ps. stay tuned for a corrected version..
Update:
http://mykenta.blogspot.com/2006/11/latest-version-of-getxdoc-dont-work-in.html

8 commenti:

Andrea Giammarchi ha detto...

// this is really bad, a global variable
// and a double operation ...
progIDs=[];
progIDs[0]='Msxml2.DOMDocument.3.0';


// change with
var progIDs=['Msxml2.DOMDocument.3.0'];



// however, I wonder why You like conditional comments so much, they are a bad practice for me ...


// I think this function should be better


function getXHR(){
function IE(AX){
var d = "Msxml2.DOMDocument.",
v = parseInt(navigator.userAgent.replace(/(^.+)MSIE (\d)(.+$)/i, "$2")),
XHR = AX && v > 4 ? new ActiveXObject(v > 5 ? d + "6.0" : d + "3.0") : null;
if(v < 6)
XHR.setProperty("SelectionLanguage", "XPath");
return XHR;
};
return window.XMLHttpRequest ? new XMLHttpRequest : IE(window.ActiveXObject);
};

kentaromiura ha detto...

Ok, thank's I correct that global var oversight.

I prefer cc to sniff for obviously reason.

I like it because for other browser are just comment and don't slow down the application.

Andrea Giammarchi ha detto...

I prefer cc to sniff for obviously reason.

because they are not standard ?

because one day some browser should change and every conditional comment script should be compromised ?

because there's always a code way to don't use conditional comment but you don't want to use them ?

Maybe You mean for obviously reason, don't use conditional comments hack ... as every hack is not a good practice.

kentaromiura ha detto...

btw, in your method you just assume that IE 5 don't have the "6.0" version.
That's wrong .

kentaromiura ha detto...

Wait, you see, for me CC are absolutly
standard in a javascript file, because they are just comment for any browser but IE.

Sniffing is bad because a browser can fake it's signature or an upgrade can change it.

..
because one day some browser should change and every conditional comment script should be compromised ?
..
no, it won't happen.


..Here I use it only because IE4 don't support try..catch .

Andrea Giammarchi ha detto...

function getXHR(){
function IE(AX){
var d = "Msxml2.DOMDocument.",
v = parseFloat(navigator.userAgent.replace(/(^.+)MSIE (\d+\.\d+)(.+$)/i, "$2")),
XHR = AX && v > 4 ? new ActiveXObject(v > 5 ? d + "6.0" : d + "3.0") : null;
if(v < 5.5)
XHR.setProperty("SelectionLanguage", "XPath");
return XHR;
};
return window.XMLHttpRequest ? new XMLHttpRequest : IE(window.ActiveXObject);
};

Andrea Giammarchi ha detto...

oooops ... I meant

function getXHR(){
function IE(AX){
var d = "Msxml2.DOMDocument.",
v = parseFloat(navigator.userAgent.replace(/(^.+)MSIE (\d+\.\d+)(.+$)/i, "$2")),
XHR = AX && v > 4 ? new ActiveXObject(v >= 5.5 ? d + "6.0" : d + "3.0") : null;
if(v < 5.5)
XHR.setProperty("SelectionLanguage", "XPath");
return XHR;
};
return window.XMLHttpRequest ? new XMLHttpRequest : IE(window.ActiveXObject);
};

Andrea Giammarchi ha detto...

damn copy and paste !!!

this is (I hope) the function

function getXHR(){
function IE(AX){
var d = "Msxml2.DOMDocument.",
v = parseFloat(navigator.userAgent.replace(/(^.+)MSIE (\d+\.\d+)(.+$)/i, "$2")),
XHR = AX && v >= 5 ? new ActiveXObject(v >= 5.5 ? d + "6.0" : d + "3.0") : null;
if(v < 5.5)
XHR.setProperty("SelectionLanguage", "XPath");
return XHR;
};
return window.XMLHttpRequest ? new XMLHttpRequest : IE(window.ActiveXObject);
};


however, it's not so different from my "old" Ajax Guide solution :)