Can the same character encoding used for html encoding be used for javascript?
No. HTML is not JavaScript.
are there any built in javascript encoding apis?
The only languages mentioned in the question tags are JavaScript and HTML. You should never generate JavaScript source code programatically from inside JS (there is always a better way to solve a problem where that is a possible solution).
For other languages, JSON encoding libraries are usually the way forward.
If you're generating HTML from JS then, again, you shouldn't be generating raw HTML. Use DOM (createTextNode
, createElement
, appendChild
and friends) instead.
Apart from the characters < , > , ' , " , & what other characters should I consider for output encoding
For HTML text nodes and safe HTML attributes, that is all you need to worry about.
For other places in HTML, it depends on the place.
Protecting against XSS when you are taking a URL as user input and putting it in an href
attribute has its own set of problems.
Protecting against XSS when you are putting user input inside an onclick
attribute (generally not a good idea) has another set of problems.
OWASP is a good starting point.