Removing + in URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • padders01
    New Member
    • Apr 2012
    • 8

    Removing + in URL

    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&Subm it=Submit"

    The code on the next page is as follows:
    Code:
    <script language="JavaScript">
    <!--
     
     
    function getValueFromUrl(name)
    {
      var url = '' + this.location;
      var idx  = url.indexOf('?');
      if(idx==-1)
        return null;
      var query = "&" + url.substring(idx+1);
      var qs = query.toLowerCase();
      var key = "&" + name.toLowerCase() + "=";
      var begin = qs.indexOf(key);
      if(begin==-1)
        return null;
      else
        begin += key.length;
      var end = qs.indexOf("&", begin);
      if (end == -1)
        end = query.length;
      return unescape(query.substring(begin, end));
    }
    // -->
    </script>
     
    </head>
    <body>
    <script>
     
        document.write("Your Phone is: "+getValueFromUrl('phone'));
     
    </script>
    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
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can use the replace function to replace all occurrences of one string with another.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      On a separate note, your getValueFromURL function could be improved, e.g.

      1. window.location .search returns the query string

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

      3. Use decodeURICompon ent in place of unescape.

      4. Use split() to split strings on characters, e.g. "&" or "=".

      Comment

      • deric
        New Member
        • Dec 2007
        • 92

        #4
        You can use urldecode()

        Comment

        Working...