Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove plus sign (+) in URL query string

I am trying get the string in the following URL to display on my webpage.

http://example.com?ks4day=Friday+September+13th

EDIT: The date in the URL will change from person to person as it's merged in by my CRM program.

I can get it to display on my webpage using the code below, the problem is the plus signs (+) come through as well.

eg. Friday+September+13th

What I need it to do is replace the plus signs (+) with spaces so it looks like this:

eg. Friday September 13th

I'm new to this so I'm having some trouble working it out.

Any help would be appreciated.

This is the code i'm using in a .js file

      function qs(search_for) {     var query = window.location.search.substring(1);     var parms = query.split('&');     for (var i=0; i<parms.length; i++) {         var pos = parms[i].indexOf('=');         if (pos > 0  && search_for == parms[i].substring(0,pos)) {             return parms[i].substring(pos+1);;         }     }     return ""; } 

This is the code i'm using on my webpage to make it display

     <script type="text/javascript">document.write(qs("ks4day"));</script> 
like image 956
user2764485 Avatar asked Sep 10 '13 11:09

user2764485


People also ask

How do you pass a plus sign in a URL query string?

Now, if you want a literal + to be present in the query string, you need to specify %2B instead. + sign in the query string is URL-decoded to a space. %2B in the query string is URL-decoded to a + sign.

How do I remove Querystring from URL?

To remove a querystring from a url, use the split() method to split the string on a question mark and access the array element at index 0 , e.g. url. split('? ')[0] . The split method will return an array containing 2 substrings, where the first element is the url before the querystring.

Is plus sign valid in URL?

Generally speaking, the plus sign is used as shorthand for a space in query parameters, but while it can be used in such a way in URLs, it isn't a good idea. The plus sign is sometimes transformed into a space or to “%20” which can cause problems for the spiders crawling your site, hence causing issues with indexation.

How do I remove values from a URL?

Just pass in the param you want to remove from the URL and the original URL value, and the function will strip it out for you. To use it, simply do something like this: var originalURL = "http://yourewebsite.com?id=10&color_id=1"; var alteredURL = removeParam("color_id", originalURL);


2 Answers

Although Bibhu's answer will work for this one case, you'll need to add decodeURIComponent if you have encoded characters in your URI string. You also want to make sure you do the replace before the decode in case you have a legitimate + in your URI string (as %2B).

I believe this is the best general way to do it:

var x = qs("ks4day");        // 'Friday+September+13th' x = x.replace(/\+/g, '%20'); // 'Friday%20September%2013th' x = decodeURIComponent(x);   // 'Friday September 13th' 

Here's an example of when it might be useful:

var x = '1+%2B+1+%3D+2';  x = x.replace(/\+/g, '%20'); // '1%20%2B%201%20%3D%202' x = decodeURIComponent(x);   // '1 + 1 = 2' 
like image 108
Luke Avatar answered Oct 03 '22 02:10

Luke


You can use replace() for this purpose

var dateString = 'Friday+September+13th'; var s = dateString .replace(/\+/g, ' '); 
like image 34
Bibhu Avatar answered Oct 03 '22 01:10

Bibhu