最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - The entity name must immediately follow the '&' (...) - Stack Overflow

programmeradmin2浏览0评论

Seems like a mon error, but none of the solutions I've found searching have proved successfull (replacing & with & is one). I have a simple Javascript (AJAX) that includes a couple of double if statements. The script works fine in both Chrome and Firefox, but not in IE9 which reports an error on the same line as Netbeans (the entity name must immediately follow the '&' (...)).

Hoping that someone here can spot the error or provide clues to where I shoud look.

function getNames(str) {
    var xmlhttp;
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        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 () {
        // The line below is what produces the error.
        if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
            var arr = xmlhttp.responseText.split(",");
            var ul = document.getElementById("names");
            var li = document.createElement("li");
            if (ul.hasChildNodes()) {
                while (ul.childNodes.length >= 1) {
                    ul.removeChild(ul.firstChild);
                }
            }

            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != ":@") {
                    var li = document.createElement("li");
                    li.innerHTML = newListItem = arr[i];
                    ul.insertBefore(li, ul.getElementsByTagName("li")[0]);
                }
            }

        }
    }
    xmlhttp.open("GET", "./ext/get_names.php?q=" + str, true);
    xmlhttp.send();
}

Seems like a mon error, but none of the solutions I've found searching have proved successfull (replacing & with &amp; is one). I have a simple Javascript (AJAX) that includes a couple of double if statements. The script works fine in both Chrome and Firefox, but not in IE9 which reports an error on the same line as Netbeans (the entity name must immediately follow the '&' (...)).

Hoping that someone here can spot the error or provide clues to where I shoud look.

function getNames(str) {
    var xmlhttp;
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        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 () {
        // The line below is what produces the error.
        if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
            var arr = xmlhttp.responseText.split(",");
            var ul = document.getElementById("names");
            var li = document.createElement("li");
            if (ul.hasChildNodes()) {
                while (ul.childNodes.length >= 1) {
                    ul.removeChild(ul.firstChild);
                }
            }

            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != ":@") {
                    var li = document.createElement("li");
                    li.innerHTML = newListItem = arr[i];
                    ul.insertBefore(li, ul.getElementsByTagName("li")[0]);
                }
            }

        }
    }
    xmlhttp.open("GET", "./ext/get_names.php?q=" + str, true);
    xmlhttp.send();
}
Share Improve this question edited Jan 22, 2012 at 15:59 Index asked Jan 22, 2012 at 15:56 IndexIndex 2,3814 gold badges33 silver badges51 bronze badges 3
  • I think you need to replace if (xmlhttp.status == 200 && xmlhttp.readyState == 4) with if (xmlhttp.status == 200 &amp;&amp; xmlhttp.readyState == 4) – Paul Tomblin Commented Jan 22, 2012 at 15:59
  • Ye, I've tried that. That broke the script pletely in all browsers. – Index Commented Jan 22, 2012 at 16:00
  • Try nesting it then: if (xmlhttp.status == 200) { if (xmlhttp.readyState == 4) { – Paul Tomblin Commented Jan 22, 2012 at 16:02
Add a ment  | 

3 Answers 3

Reset to default 4

You can enclose the script within a CDATA section:

<script type="text/javascript">
//<![CDATA[
document.write("&&");
//]]>
</script>

Unless you have a particular reason, the cleanest solution is usually moving your javascript code to an external file.

Besides avoiding most odd problems with browsers interpreting included javascript differently, it also makes things more cacheable.

Jukka K. Korpela explained an IE9 quirks mode behavior in a ment to this answer

This is bug in IE 9 (in Quirks Mode), since by the spefications, &#x2022 is valid here. But it has always been good and remended practice to end all entity references (like &nbsp;) and character references (like &#233;) with a semicolon, partly because omitting it has triggered various browser bugs.

I disagree on what the specification says, but IE9 quirks mode may be the culprit.

发布评论

评论列表(0)

  1. 暂无评论