An extra measure to preventing Cross-Site Scripting has now become a standard ready to be implemented. It’s as easy as including this header in your sites
Content-Security-Policy: script-src 'self'; img-src 'none'
With this, external scripts and images won’t be loaded on your site. This new header however doesn’t mean you’re fully protected once you include it; you’ll still want to encode your strings serverside, as you don’t want *any* HTML tags to be injected. Also, older browsers still will be a target as they don’t understand the header (but that on the other hand could be a huge argument to pushing clients towards the newest version of their browser).
More on Content Security Policy: An Introduction To Content Security Policy →