LoginSignup
2
1

More than 3 years have passed since last update.

IE互換表示の微妙さとXMLHttpRequestの罠

Last updated at Posted at 2020-01-08

やりたいこと

  • HTTPヘッダをjsで取得したい

前提

  • IE11を使うが、互換表示を強制的にかけられるのでIE5相当のjsしか使えないハズ
  • ならばXMLHttpRequestも無理か…と思ってたら微妙に使えた

結論

  • 互換表示でもXMLHttpRequestは叩ける。本来IE5にはないはずなんだが。
  • String.trim()はIE5にはないって言ってくる。
  • addEventListenerはIEにはないらしい。

この一貫性のなさ!!納得できねぇ~!

AddEventListenerのページに、IEでのイベントの拾い方書いといてもらえたらなぁ
https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest/loadend_event

ここ見ないとわからんかった
https://www.javadrive.jp/ajax/http/index5.html

実装例

ActiveX版。本来これしか動いたらあかんと思うのだが
// ActiveX読み込み
var httpReq = new ActiveXObject('Msxml2.XMLHTTP.6.0');
httpReq.onreadystatechange = cons;
httpReq.open('get', location.href, true);
httpReq.send(null);

function cons(e){
    if((httpReq.readyState == 4) && (httpReq.status == 200)){
        console.log(httpReq.getAllResponseHeaders());
    }
}

なおActiveXObjectのバージョンはどっちの指定でもいけた
ActiveXObject('Msxml2.XMLHTTP.6.0');
ActiveXObject('Microsoft.XMLHTTP');
なんとXMLHttpRequestも叩ける
var httpReq = new XMLHttpRequest();

// でもaddEventListenerはない
httpReq.onreadystatechange = cons;
httpReq.open('get', location.href, true);
httpReq.send(null);

function cons(e){
    if((httpReq.readyState == 4) && (httpReq.status == 200)){
        console.log(httpReq.getAllResponseHeaders());
    }
}

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1