다음 스크립트는 Safari, Chrome 및 Firefox에서 제대로 실행되고 작동하지만 IE8에서는 작동하지 않습니다. 불행히도 IE8은 내 타겟 브라우저 중 하나이기 때문에 약간 문제가 있습니다. 아약스에 대한 많은 경험이 없기 때문에 어디서부터 시작해야할지 모르겠습니다.IE8에서 Ajax 스크립트가 실패합니다.
나는 IE가 라인 15 (**로 표시)에 오류를보고하는데, 실제로는 그 라인을 보지 못하도록 if-else가 이해하지 못한다고 지적했다.
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)) –