Skip to content Skip to sidebar Skip to footer

Replace + With %20 In URL Generated

I am using the below HTML code to connect a user to a chat, but I need any spaces in the URL generated to become %20 and not +, because + is a valid character in a nickname, so som

Solution 1:

Try using encodeURIComponent(str) function.

var test = "Paul John";
var result = encodeURIComponent(test );
//result: "Paul%20John"

To replace the content just before submitting the form use this jquery code:

$(document).ready(function(){
    $('.form-horizontal').submit(function(){
        var encoded = encodeURIComponent($('#nickname').val().trim());
        $('#nickname').val(encoded );
    });
});

Solution 2:

This happens because '+' indicates a space when using application/x-www-form-urlencoded content. As you are actually submitting a form and therefore sending form-urlendoded data, this is correct. If you enter a '+' in the data it should also get encoded (to '%2B') allowing you to distinguish this from any '+' character added as part of form encoding


Post a Comment for "Replace + With %20 In URL Generated"