By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,322 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

Removing + in URL

P: 8
I am passing information from one page to another in the URL

The URL is as follows: "http://localhost/order.asp?phone=Apple+iPhone+5+64gb+White+-+%C2%A3500&Submit=Submit"

The code on the next page is as follows:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="JavaScript">
  3. <!--
  4.  
  5.  
  6. function getValueFromUrl(name)
  7. {
  8.   var url = '' + this.location;
  9.   var idx  = url.indexOf('?');
  10.   if(idx==-1)
  11.     return null;
  12.   var query = "&" + url.substring(idx+1);
  13.   var qs = query.toLowerCase();
  14.   var key = "&" + name.toLowerCase() + "=";
  15.   var begin = qs.indexOf(key);
  16.   if(begin==-1)
  17.     return null;
  18.   else
  19.     begin += key.length;
  20.   var end = qs.indexOf("&", begin);
  21.   if (end == -1)
  22.     end = query.length;
  23.   return unescape(query.substring(begin, end));
  24. }
  25. // -->
  26. </script>
  27.  
  28. </head>
  29. <body>
  30. <script>
  31.  
  32.     document.write("Your Phone is: "+getValueFromUrl('phone'));
  33.  
  34. </script>
  35.  
It writes the following:

Your Phone is: Apple+iPhone+5+64gb+White+-+£500

How do I get it to remove the + inbetween the words and replace it with a space
Nov 21 '12 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,430
You can use the replace function to replace all occurrences of one string with another.
Nov 21 '12 #2

acoder
Expert Mod 15k+
P: 16,027
On a separate note, your getValueFromURL function could be improved, e.g.

1. window.location.search returns the query string

2. window.location.search.substring(1) without the "?"

3. Use decodeURIComponent in place of unescape.

4. Use split() to split strings on characters, e.g. "&" or "=".
Nov 22 '12 #3

P: 92
You can use urldecode()
Nov 23 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.