I have a classic ASP which is coded in VB script and it also has Javascript code in the script tag.
VS Script has an array and some row contains a single quote. And this VB Script array is being passed to Javascript. As some string values contains single quotes, the webpage does not work after the string is passed to Javascript.
I tried to escape single quotes in VB script before passing to JavaScript
Dim escapeInvalidString
escapeInvalidString = Replace(objrec.Fields("Name"), "'", """chr(39)""")
vbStr = escapeInvalidString
Also tried
escapeInvalidString = Replace(objrec.Fields("Name"), "'", "''")
I tried in Javascript without escaping in VB Script as well
var jsStr ="";
jsStr = '<%= vbStr %>'.replace(/'/g, "\\'");
Also tried this.
jsStr = '<%= vbStr %>'.jsStr.replace(/\"/g,'\\"');
I have a feeling that I need to escape single quotes in VB Script parts but the above did not work. Any tips would be very appreciated.
Use Escape on the server-side and unescape on the client-side. They are compatible and both are Unicode (well, UCS-2 in fact) compliant.
var jsStr = unescape('<%= Escape("foo ' bar '") %>');