TechnoBuzz

A Techno Blog, mainly about Java

escaping characters

I find that if you are using HTTP GET you will run into a lot of troubles with your url string with characters such as quotes, colons, and others. The way to get around that is to use HTTP POST. But, if you still want to use GET, the best way is to escape the characters.

“URL encoding is the practice of translating unprintable characters or characters with special meaning within URLs to a representation that is unambiguous and universally accepted by web browsers and servers”

Here are a few javascript solutions on how to encode url:

Javascriptwriter: You can convert any string to a URL-encoded string (suitable for transmission as a query string or, generally speaking, as part of a URL) using the JavaScript functions escape,encodeURI and encodeURIComponent.

From an ASP dot net blog  : Understand that in asp.net, virtual path in the URL with colon (:) is not allowed. To solve this problem, i replace the colon (:) with ASCII value for colon : (please check http://www.asciitable.com/)

If you are working with javascript,  it provides a number of functions that encode and decode special characters

MSDN JScript Reference – escape()encodeURI()encodeURIComponent()

Mozilla Developer Core Javascript Guide – escape()encodeURI()encodeURIComponent()

ASCII Table – http://www.asciitable.com/

W3C’s URIs, URLs, and URNs: Clarifications and Recommendations 1.0 – http://www.w3.org/TR/uri-clarification/

Iframe JQuery http://stackoverflow.com/questions/5044210/jquerys-attr-escaping-ampersands, if you are using jQuery 1.6 and up, you want to use .prop() rather than .attr():

One thing to consider:

//import org.apache.commons.lang.StringEscapeUtils;
StringEscapeUtils.escapeJavaScript(out, str)

Documented at :
http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringEscapeUtils.html#escapeJavaScript%28java.lang.String%29

References:

http://www.freeformatter.com/javascript-escape.html#ad-output

 

February 16, 2013 Posted by | Uncategorized | Leave a comment