Improved the escaping of variables used in JS code

Some of these variables where not being escaped, or only partially.