If you were outputting to HTML, use the Server.HTMLEncode
.
<%=Server.HTMLEncode(Request.QueryString("url")) %>
However, it appears you are outputting to JavaScript.
The best way would be not to do this. See my question and the answer here, but basically use data-
attributes so you can use the code above, and then fish out the value using JavaScript:
<div id="dataExample" data-url="<%=Server.HTMLEncode(Request.QueryString("url")) %>" />
Check out the OWASP XSS (Cross Site Scripting) Prevention Cheat Sheet for further information. This also has another way of doing it, but it is much more complex which always is inverse to security. This involves encoding using \x00
format. However, as this involves writing some custom code it means you cannot rely on any inbuilt functions, which means it is prone to error. I would heavily recommend the HTML approach.