Finding spaces in a string variable and replacing it with "%20"

Finding spaces in a string variable and replacing it with "%20"

I need to pass a variable into a URL but need to replace the spaces with
 
$.ajax({
   beforeSend: function(){$('#loading').show();},
   type: "GET",
    url: "http://server/cgi-bin/findwhoisonduty.pl?host="+groupName,
    dataType: "text/xml",



 
What's the best way to do that?