DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=30082>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=30082

escape to javascript for use as href in window.open

           Summary: escape to javascript for use as href in window.open
           Product: Commons
           Version: unspecified
          Platform: All
               URL: http://www.blue-moose.net
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Lang
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


Could the following code be added to StringEscapeUtils?  

The use case is, you have a javascript routine which runs a window.open with 
parameters in the URL.  A reason for this kind of functionality would be 
modularized poping of windows with parameters.  Most characters are handled 
nicely with the javascript escape method.  However backslash, double quote, 
single quote and percent are not handled cleanly.  This method resolves the 
issues with this situation.

Thanks

Edgar

/**
 * encode a string for an href parameter escaped to a javascript function
 * which will perform a window.open().
 * usage in JSP
 *
 * <a href='javascript:myFunction("/myURI?myVarName=" + 
 * escape("<%=StringEscapeUtils.jsEscape(myVariable)%>"));'>
 */
public static String jsEscape(String s) {
        if (s == null) {
                return "";
        }
        char [] c = s.toCharArray();
        StringBuffer result = new StringBuffer();
                for (int i = 0; i < c.length; i++) {
                switch (c[i]) {
                        case '\\' :
                                result.append("\\\\");  // escape
                                break;
                        case '\'' :
                                result.append("\\%27"); // single quote
                                break;
                        case '\"' :
                                result.append("\\%22"); // double quote
                                break;
                        case '%' :
                                result.append("%25");   // percent
                                break;
                        default :
                                result.append(s.substring(i,i+1));
                }
        }
        return result.toString();
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to