purazumakoiの[はてなブログ]

技術メモから最近はライフログも増えてきてます。

XmlDocumentオブジェクトを返す関数

Ajaxイン・アクションのサンプルを実行してみた。

■リスト2.8 getXmlDocument()関数

XMLDocument オブジェクトは本来xmlを取得する目的に使われるっぽいけど
今回はブラウザの判別程度にしか使えない

demo

demoはコチラ
※注:alertが二つでます


getXmlDocument()関数

 function getXMLDocument() {
  var xDoc=null;
  if(document.implementation && document.implementation.createDocument){
   xDoc = document.implementation.createDocument("", "", null); //Mozilla/Safariとか
   alert("Mozilla/Safariとか");
  }
  else if(typeof ActiveXObject != "undefined") {
   var msXmlAx = null;
   try {
    msXmlAx = new ActiveXObject("Msxml2.DOMDocument"); //新しいIE
    alert("新しいIE");
   }catch(e) {
    msXmlAx = new ActiveXObject("Msxml.DOMDocument");  //古いIE
    alert("古いIE");
   }
   xDoc = msXmlAx;
  }
  if(xDoc == null || typeof xDoc.load == "undefined") {
   xDoc = null;
  }
  return xDoc;
 }

■リスト2.9 getXMLHttpRequest()関数

XMLHttpRequestオブジェクトは、サーバとのHTTP通信を行うためのもの。
今回はブラウザの判別用です。
XMLHttpRequestオブジェクトをサポートしてないブラウザ(IE6以下)はActiveXオブジェクトを入れます。

demo

demoはコチラ
※注:alertが二つでます


getXMLHttpRequest()関数

 function getXMLHttpRequest() {
  var xRequest = null;
  if(window.XMLHttpRequest) {
   xRequest = new XMLHttpRequest(); // Mozilla/Safari/Opera/IE7も
   alert("Mozilla/Safari/Opera/IE7も");
  }
  else if(typeof ActiveXObject != "undefined"){
   xRequest = new ActiveXObject("Microsoft.XMLHTTP"); // IE6以下
   alert("IE6以下");
  }
  return xRequest;
 }

他にもXMLHttpRequestの使い方の説明ページは沢山ありますね。
参考:
XMLHttpRequest の使い方 - WebOS Goodies



Ajaxイン・アクション
Ajaxイン・アクションDave Crane

おすすめ平均
stars内容は良い
stars初心者向けでない、中・上級者向けの本
starsAJAX黎明期の指南書
starsAjax 開発者必携の解説書!

Amazonで詳しく見る
by G-Tools