我有这样的当前页面网址:
https://example.com/product/view/faqlist/category_id/2/faq/219/
URL将是动态的。在这里,category_id和faq是参数。我想获得这个价值。
我尝试下面的代码。但是,仅当我只添加一个查询字符串时,它才起作用。它不能多次工作。
我想获取category_id的值和常见问题查询字符串的值。我怎样才能做到这一点?
注意:我不想将查询字符串从“ /”更改为“?”
var pathname = window.location.pathname.split("/");
var filtered_path = pathname.filter(function(v){return v!==''});
var filename = filtered_path[filtered_path.length-1];
最佳答案
使用String#split
,通过使用Array#indexOf
的参数索引可以获取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);
关于javascript - 从当前URL获取所有斜杠查询字符串值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57950871/