The W3C HTML validator reports errors in lines which are inside script <script>
tags. It's creating a lot of noise in the validation output. I can wrap my own script in CDATA but I have a lot of script added dynamically by third party controls.
Is there an HTML validator whi开发者_Go百科ch can ignore everything in all <script>
sections?
Short Bad Answer
If you wish to continue to use the w3 validator but get rid of certain errors regarding html in script tags, you can comment your JavaScript as shown in this guide. This is clearly a hack and is not recommended.
Long Good Answer
The main point of a validator is to ensure your code keeps to standards. The documentation for the w3 validator points you to this guidance and the w3 itself has a guide on keeping html within script to standards.
Personally, I don't see a point of a validator that selectively ignores some standards. You can't know how a random browser is going to implement the w3 standard and just because the major browsers assumedly do not do anything wrong when ignoring errors embedded in script tags, that doesn't mean there aren't browsers that don't conform to standards more closely. Furthermore, there is no guarantee that major browsers won't change their implementation in the future to be closer to standards and thus break your code. It is better to fix the errors you are getting rather than ignore them.
Solution:
Remove the offending third party scripts while you're validating the HTML.
It might be that Michael Robinson's suggestion or Rupert's Bad Short Answer can be done programmatically, though it might be painful to program.
If you can put a proxy or filter in front of your page that strips or modifies the script tags on the fly, the validator will not see the scripts.
Unfortunately, stripping the scripts is only easy if you've got valid XHTML, in which case of course you mightn't really need the validator...
Aside from the fact that this might be fun to try, I'm in favor of Rupert's Long Good Answer.
精彩评论