使用jQuery拆分URL

时间:2021-12-28 作者:DK Thakur

Can i get the help of you guys as i want i can do it with url

我需要拆分URL。并使用最后一个元素作为job\\u id,然后传递;JKDSBD45Jubjkaaassyt“;作为job\\u Id。

URL: https://dev/job/?job_id=jkdbsbd45JubJKAAASSYT
需要按以下方式创建此URL。

Need: https://dev/job/jkdbsbd45JubJKAAASSYT

Thanx in advance

1 个回复
最合适的回答,由SO网友:Buttered_Toast 整理而成

这不是一个与WordPress相关的问题,但你可以这样做

// creates a object from the array, one of the properies (search) contains the query
let url = new URL(\'https://dev/job/?job_id=jkdbsbd45JubJKAAASSYT\');
// will create a object of all availalble query properites
const urlSearchParams = new URLSearchParams(url.search);
const params = Object.fromEntries(urlSearchParams.entries());
// remove ? and everything after from url
url = url.href.substring(0, url.href.indexOf(\'?\'));

// url + params.job_id is the final url, did a console.log so you could see it in the devtools console
console.log(url + params.job_id);