2

What I'm trying to do is fetch a single piece of a string without using the hashtag element in the url. I already have a functioning code but it needs altering. So, how do I fetch any part of the url after ?.

Say I have ?fx=shipment+toys/fish-fix-fx/ as my url string; I want the button to show if shipment or fish or fx was my choice of selections for example.

Buttons showing with hastag: http://jsfiddle.net/66kCf/2/show/#iphone
Original JSFiddle (buttons not showing): http://jsfiddle.net/66kCf/2/

I want the iPhone buttons to show if fix was my choice: http://jsfiddle.net/66kCf/2/show/?fx=shipment+toys/fish-fix-fx/

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139

4 Answers4

1

try doing it with .split() and.match() like this...

var keys = window.location.href.split('?'); 
if (keys[1].match(/(fix|fish|fx)/))
{
    $("#linkdiv").append(nextLink);
    $("#linkdiv1").append(nextLink);
    $("#linkdiv2").append(nextLink);
}  

demo button showing : http://jsfiddle.net/LbKmf/show/?fx=shipment+toys/fish-fix-fx/
demo button not showing: http://jsfiddle.net/LbKmf/show/?reigel

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
0

Is this what your looking for:

"?fx=shipment+toys/fish-fix-fx/".split(/[\?=+\/-]/g);
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • Could you show me the implementation with the original code sir –  May 25 '14 at 01:55
  • what is that none of the links above works on my end? – Dalorzo May 25 '14 at 02:04
  • You should see three buttons with `#iphone` in the url (http://jsfiddle.net/66kCf/2/show/#iphone) –  May 25 '14 at 02:06
  • The buttons will not show up in (http://jsfiddle.net/66kCf/2/) because the hashtag iPhone (`#iphone`) is not in the url. –  May 25 '14 at 02:11
  • Your sample does not work if you want share a working or more clear sample I will take a look so far what you have is not usable. – Dalorzo May 25 '14 at 02:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54367/discussion-between-user2680614-and-dalorzo). –  May 25 '14 at 02:18
0
Community
  • 1
  • 1
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

Generally, Javascript doesn't have a built-in functionality for query string parameters. You can use string manipulation on window.location.search to get your parameters out of the URL string. Note that location.search includes the ? character too.

Something like this should do:

   var queryString = function () {
          // Anonymous function - executed immediately
          // get rid of the '?' char
          var str = "?fx=shipment+toys/fish-fix-fx/";
          var query = str.substring(str.lastIndexOf('=')+1,str.indexOf('/'));
          var vars = query.split("+");
          for (var i=0;i<vars.length;i++){
              console.log(vars[i]);
          }
          return vars;
        } ();
Navid
  • 11
  • 4
  • I'm open to JQuery as well. But if you could show me how to implement the `window.location.search` then that would solve the issue. –  May 25 '14 at 02:01
  • I've added a piece of code that may suit your needs. It extracts the parameters and return them in an array (I'm also printing them on the console so that you can see the results for yourself). – Navid May 25 '14 at 02:27