If you're using ASP.net (as it looks like you are), use AjaxHelper.JavaScriptStringEncode
or HttpUtility.JavaScriptStringEncode
to output the string correctly.
var userName='<%= HttpUtility.JavaScriptStringEncode(Request.ServerVariables("LOGON_USER"))%>';