views:

388

answers:

2

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(dropdownhtml);
    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?

+1  A: 

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

ridecar2
My mistake - I missed that off when copying, thanks
Chris
+3  A: 

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.")

T.J. Crowder
It is being included from a XHTML webpage... doctype transitional xhtml... I will update question with how I include it
Chris
@Chris: XHTML is XML (in theory, some browsers fail there), so the answer's pretty much the same.
T.J. Crowder
Why do I not have to do this for any of my other javascript files? Is it because I am not including the file in the `head`?
Chris
@Chris: If you're referencing an external file (as your updated question indicates), you don't have to do it at all (one of the reasons for using an external file! :-) ). You only need to wrap the Javascript in a `CDATA` section if the script itself is embedded in the XHTML. (See the top of my answer).
T.J. Crowder
@Chris: If you can pare your page down to a minimalist test page that demonstrates the problem, you can update your question quoting the full content of the page, and we may be able to help you figure out what's going on. But don't post hundreds of lines, it needs to be the *minimum* necessary to recreate the problem. (And in my experience, if I start creating that minimalist test case, I find the error myself. :-) )
T.J. Crowder
I found the issue!! I realised I had used `<script>` tags inside my javascript file... What an idiot lol
Chris
Please update your answer with that and I'll accept it :)
Chris