What you're looking for is Content Security Policy. This is implemented by output of a HTTP response header:
Content-Security-Policy: default-src https://cdn.example.net; frame-src 'none'; object-src 'none'
This is definitely worthwhile including on your website, particularly in cases where you are allowing user authored HTML content. Yes, strip out as much script content as you can using a sanitizer such as Google Caja, but as the languages of the web are constantly evolving so do the XSS attacks to exploit it. This is why you should always implement a Content Security Policy to protect against this threat. Google implement this on Gmail to prevent emails that have a new way of evading their XSS filters from doing any damage.
Note that if you are not allowing HTML content from untrusted sources then you should be encoding on output (it is not tedious if it is properly done as a site is developed). The CSP in this case is a backup for any coding mistakes - if you do a good vulnerability assessment on your website it will still pick up potential XSS flaws, even if it is protected by a CSP. The CSP will give you time to fix it without it being exploited in the meantime. For example, in HTML you would HTML encode (e.g. convert <
to <
). Please see the OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet for further details. Input validation can also be done as a second line of defense. This is where you would, for example, prevent letters from being accepted if a field was asking for a phone number. You should do this via whitelisting - only accept the characters you are expecting rather than rejecting ones you think are bad. So rather than adding all letters to a blacklist, you would add numbers and possibly the -+#()
characters to a whitelist.
Always make sure to correctly encode depending on output context. Output to JavaScript requires completely different encoding than HTML. My recommendation is to always output to HTML and then retrieve the values using the DOM if you require server set variables to be used in client side script. The possible exception to this would be output to JSON where a tried and tested JSON encoder is used.
Note that Internet Explorer does have partial support for CSP. Please see this page for a full breakdown of browser support.