次のスクリプトはSafari、Chrome、Firefoxで正常に動作しますが、IE8では正常に動作しません。残念なことにIE8は私がターゲットとするブラウザの1つなので、これは少し問題です。私はAjaxに関する多くの経験がないので、どこから探し始めるかは分かりません。IE8でAjaxスクリプトが失敗する
IEは、if-elseがその行を見てもそれを止めなければならないので、実際には意味をなさないエラー15行目(**でマークされています)を報告しています。
function getNames(str) {
var xmlhttp;
// Clear previous queries
if(str.length == 0){
document.getElementById("txtHint").innerHTML = "";
return;
// Due to the high number of possible hits we'll demand 3 chars
// before we start querying.
}else if(str.length < 3){
return ;
}
if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}else{ // code for IE6, IE5
**xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");**
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.status == 200 && xmlhttp.readyState == 4){
// String from get_names.php is comma separated
var arr = xmlhttp.responseText.split(",");
// The UL list we want our names in
var ul = document.getElementById("names");
// Clear the list for each key in
if(ul.hasChildNodes()){
while(ul.childNodes.length >= 1){
ul.removeChild(ul.firstChild);
}
}
// Step trough the String in Array form
for(var i = 0; i < arr.length; i++){
// :@ means that we've reached the end of applicable names.
if (arr[i] != ":@") {
var li = document.createElement("li");
li.innerHTML = newListItem = arr[i];
// Inserts the current name into the list.
ul.insertBefore(li, ul.getElementsByTagName("li")[0]);
}
}
}
}
xmlhttp.open("GET", "./ext/get_names.php?q=" + str, true);
xmlhttp.send();
}
ストックオプション:これのためにjQueryなどのフレームワークを使用することを検討しましたか?彼らはあなたのためにこの十字架のすべての宣教師を世話します。 – Jamiec
あなたの質問に直接答えはありませんが、おそらくjsフレームワーク(jQuery/mootools/...)を考慮する必要があります。これらはクロスプラットフォームで動作し、頭痛を軽減します –
アクティブなxオブジェクトが利用可能かどうかをテストするロジックを追加できますか?(if(window.ActiveXObject)) –