shantanuo
shantanuo

Reputation: 32316

How to replace a space in a URL in JavaScript?

These are the 2 relevant lines from a function written in JavaScript:

var v_depttime = document.getElementById("EDDepttime").value ;
url = url+"?select_bno="+v_busno+"&select_depttime="+v_depttime ;

It sends the select_depttime as 2010-01-24 14:30:00 and I want it to be an URL encoded string like 2010-01-24%2014:30:00

How is it done in JavaScript?

Upvotes: 5

Views: 11118

Answers (2)

Gumbo
Gumbo

Reputation: 655239

Use encodeURI or encodeURIComponent.

Upvotes: 13

Guffa
Guffa

Reputation: 700322

Use encodeURIComponent:

url = url +
  "?select_bno=" + encodeURIComponent(v_busno) +
  "&select_depttime=" + encodeURIComponent(v_depttime);

Upvotes: 11

Related Questions