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/

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

0

Is this what your looking for:

"?fx=shipment+toys/fish-fix-fx/".split(/[\?=+\/-]/g); 

6 Comments

Could you show me the implementation with the original code sir
what is that none of the links above works on my end?
You should see three buttons with #iphone in the url (jsfiddle.net/66kCf/2/show/#iphone)
The buttons will not show up in (jsfiddle.net/66kCf/2) because the hashtag iPhone (#iphone) is not in the url.
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.
|
0

Comments

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; } (); 

2 Comments

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.
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).