Search code examples
replacexpagesxpages-ssjsencodeuricomponent

Using encodeURIComponent in XPages


In the W3Schools they give this example:

var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
var res = encodeURIComponent(uri);

Which does what I want however, when I run it I get an error that encodeURIComponent is not implemented.

try{    
    var str:String = "Contracts and Leases";
    var rtn = encodeURIComponent(str);
}catch(e){
    println(e)
}

added the above to a button i get the not implemented error. Is there an equivalent I just need to replace the spaces with %20s.


Solution

  • Use java.net.URLEncoder.encode(stringToEncode, "utf-8") so in your case do this:

    try{    
        var str:String = "Contracts and Leases";
        var rtn = java.net.URLEncoder.encode(str, "utf-8");
    }catch(e){
        println(e)
    }