开发者

Missing } in XML expression

I have an external javascript file that I want to, upon include, write some HTML to the end of the web page.

Upon doing so though I get the error Missing } in XML expression on the line that uses dropdownhtml.

Here is my code

var dropdownhtml = '<div id="dropdown"></div>';

$(document).ready(function(){

    //$(document).append(drop开发者_Go百科downhtml);
    alert(dropdownhtml);
});

The XHTML webpage that includes this file does so like this:

<script type="text/javascript" src="/web/resources/js/dropdownmenu.js"></script>

Doing either append or alert throws up the same error, what is going wrong?


I got this error because I called an external JavaScript within an existing JavaScript, so ended up with:

<script type="text/javascript">
<script type="text/javascript">
code
</script>

code
</script>


Edit Your update changes the question a bit. :-)

There's nothing wrong with your quoted Javascript or with the script tag that includes it, the problem must lie elsewhere on the page.

The old answer:

If you're including Javascript inside an XML document, you must wrap it up in a CDATA section, or you'll run into trouble like this because the XML parser neither knows nor cares about your Javascript quotes, and instead seems markup (your <div>s in the string).

E.g.:

<foo>
<bar><![CDATA[
    var dropdownhtml = '<div id="dropdown"></div>';

    $(document).ready(function(){

        //$(document).append(dropdownhtml);
        alert(dropdownhtml);
    });
]]></bar>
</foo>

Naturally you need to ensure that the ]]> sequence never appears in a string (or comment, etc.) your script, but that's quite easy to do (for instance: "Be sure to interrupt the end sequence with a harmless backslash like this: ]]\>; that escape just resolves to > anyway.")


There's definitely a missing ); at the end of your code sample. Don't get where there may be a missing } though.


I have empty script on my page

<script src=""></script>

And this leads to such error

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜