Chris Muench
Chris Muench

Reputation: 18318

javascript window.location do I need to escape?

    var delivery_date = $("#year").val()+'-'+$("#month").val()+'-'+$('#day').val();
    var delivery_time = $("#delivery_time").val();
    var zone = $("#zone").val();
    window.location = window.location+'/'+delivery_date + '/'+ delivery_time + '/'+ zone;

Do I need to escape the parameters in window.location = window.location...etc

zone could be something with spaces, quotes,..etc

Upvotes: 1

Views: 11453

Answers (3)

Jon
Jon

Reputation: 437386

You need to properly url-encode the parameters. See here for more information; encodeURIComponent should work fine.

Upvotes: 3

ezmilhouse
ezmilhouse

Reputation: 9121

if you want to have the php url_encode and url_decode behaviour use:

function url_encode(str) {
    str = (str + '').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function url_decode(str) {
    return decodeURIComponent((str + '').replace(/\+/g, '%20'));
}

Upvotes: 1

bpierre
bpierre

Reputation: 11457

Use encodeURIComponent.

var zone = encodeURIComponent($("#zone").val());

Upvotes: 0

Related Questions