views:

147

answers:

5

Hi
I have some JavaScript code in my php website.
This code uses jQuery, and builds a < select > menu using ajax calls.

Here is the code

sel.append('<option value="' + data[i].id + '">' + data[i].nombre + '</option>');

And this gives me the following warning

line 240 column 82 - Warning: '<' + '/' + letter not allowed here

Does anyone know how can I fix this warning, so my html validates? Thanks

+4  A: 

Put the javascript in an external file.

Galen
+1 always best for any significant amount of code
bobince
A: 

Put <!-- and --> around your code.

powtac
That is a hack for Netscape 2.0 era browsers … that won't solve this problem.
David Dorward
Has no effect against HTML4 (or HTML 3.2!). Breaks script in XML.
bobince
But it works, the browsers ignore it.
powtac
It doesn't work (for the given problem) and browsers ignore the whole script if it is XHTML (unless it is served up at text/html). Argh. I hate XHTML as text/html.
David Dorward
`<!--...-->` is an XML comment literal in JavaScript 1.6+
Eli Grey
+1  A: 

If you are writing javascript in the html/xhtml page, you can enclose the javascript in CDATA

<script type="text/javascript">
/* <![CDATA[ */
console.log("..js code here..");
/* ]]> */
</script>
vsr
A `</` sequence inside an XML CDATA section is still invalid when served as HTML, which XHTML almost always is. A `</script` sequence inside the CDATA section will still break HTML browsers.
bobince
+1  A: 

To include code which isn't encoded as XML in an XHTML document (and I'm guessing that's what you're trying to do) you need to do something like the following:

<script type="text/javascript">
//<![CDATA[

alert("<This is now valid XHTML>");

//]]>
</script>
FinnNk
This is a tolerable solution for dealing with inline script in XHTML — but the validator would complain about the `<option` instead of the `</`, so the question is about HTML.
David Dorward
+5  A: 

The issue is that any </ sequence — known as ETAGO — ends a CDATA element such as a <script>. You can get away with </ in browsers but not </script.

The simplest workaround is to break up the </ sequence with a backslash-escape:

sel.append('<option value="' + data[i].id + '">' + data[i].nombre + '<\/option>');

However this line still has problems, because you aren't HTML-escaping your id and nombre values. If they may contain <, & or ", you've just built yourself a client-side XSS vulnerability!

So either HTML-escape your text values before putting them into strings, or, perhaps simpler, just use the standard DOM:

sel.append(new Option(data[i].nombre, data[i].id));

bobince
(Using standard DOM)++. innerHTML is quick and dirty, but mostly dirty.
David Dorward
Hi bobince, I think your answer is the best choice. I tried the sel.append (new Option...) and worked.Now my "sub-question" would be, how do you add an option with some attributes? like class="firstoption", an id, etc.?Hope you've understood me
Enrique
You can say `var option= new Option(...)` then treat it as any other DOM element, for example `option.className= 'firstoption';` or jQuery `$(option).addClass('firstoption');`.
bobince
That's it !! thanks bobince
Enrique