Ankita Patel
Ankita Patel

Reputation: 251

Get all slash query string value from current URL

I have current page URL like this :

https://example.com/product/view/faqlist/category_id/2/faq/219/

URL will be dynamic. Here, category_id and faq are parameters. I want to get this value.

I tried this below code. But, It's only working when, I just add one query string. It's not working in multiple.

I want to get value of category_id and faq query string's value. How can I do this?

Note : I don't want to change query string from "/" to "?"

var pathname = window.location.pathname.split("/");
var filtered_path = pathname.filter(function(v){return v!==''});
var filename = filtered_path[filtered_path.length-1];

Upvotes: 2

Views: 1567

Answers (1)

ellipsis
ellipsis

Reputation: 12152

Use String#split and the index of the parameters by using Array#indexOf can get you the values of the query string values of the URL.

var url = "https://example.com/product/view/faqlist/category_id/2/faq/219/";
var arr = url.split('/');

var a = arr[arr.indexOf('category_id') + 1];
var b = arr[arr.indexOf('faq') + 1];

console.log(a);
console.log(b);

Upvotes: 4

Related Questions