views:

172

answers:

4

Further to my last question, I'd like to know if current browsers can read attribute values without any quotes, even if the value contains slashes, like:

 <script src=/path/to/script.js type=text/javascript></script>

Will it work without causing a DOM parsing error and corrupting the entire document?

+8  A: 

It's valid HTML 5, but not valid XHTML. As a best practise, I would always include the quotes, even if you're not using XHTML.

Thorarin
Would current and future browsers be alright with it even when the DOCTYPE is XHMTL? I know its not ideal but I just need to know whether it will work.
Jenko
I think browsers in general will have no trouble with unquoted attribute values in many years to come, unless maybe if the value contains spaces. Browsers may be standards-compliant, but they are still trying to be as bulletproof as possible concerning syntax errors and the like.
Arve Systad
"Bulletproof". Excellent. Thank you for your confirmation. This is conclusive.
Jenko
It's safe-ish, but why would you *need* this? Just creating a potentially massive problem for yourself later for... what benefit exactly?
annakata
What problems could arise in the future? If it doesn't break DOM parsing, then what else could it affect?
Jenko
It could very well break DOM parsing in the future. If you want to ensure future support, it's best to stick to standards and not what the browsers currently support.
tschaible
+3  A: 

It's in the spec so you should be safe:

"In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them."

edeverett
I know, but thats the HTML 4 spec, and I've no idea on how well browsers actually read it.
Jenko
As slashes is not in the list of allowed characters, the values are never safe without quotation marks.
Guffa
A: 

It is valid in HTML, but is not considered best practice. It isn't XHTML valid so if that is what you aim for I'd suggest enclosing it with quotes. It is just habit for me now.

Oliver Stubley
+1  A: 

It is valid in HTML4/5 as long as their are no spaces in the value. It is not valid in XHTML though it is still properly rendered on some (all?) browsers.

Using a quick sample HTML doc (at end of answer), I tested this with Firefox 3.5 Beta 4, Google Chrome 2, and IE 7. The sample doc was also tested with HTML 4 Strict, HTML 4 Transitional, HTML 5, and XHTML doc types. All were rendered successfully.

Some other things to consider...

Many modern browsers attempt to render pages in standards compliant mode. If you're trying to use XHTML, and you use unquoted values, browsers will revert back to quirks mode, possibly affecting the rendering of your otherwise standards compliant documents. As browsers become more and more standards compliant as well, future specs may look to deprecate unquoted values.

Additionally, in some web frameworks, applications may be parsing templates prior to serving them up. Again, if these parsers are expecting valid XHTML, this type of thing could cause failures.

As long as you are sticking to HTML and not XHTML you should be fine without quotes, but there is no guarantee what will happen with XHTML browser to browser. Adding quotes seems like a small cost and keeps your code generally in line with standards for the future.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<title>No Quotes Test</title>
</head>
<body>
<input type="text" value="with/quotes and spaces" ></input>
<input type=text value=no/quotes and spaces ></input>
<input type=text value=no/quotes_no_spaces ></input>
</body>
</html>
tschaible