JS常用url 链接入参出参 拼接新参数提取参数 可直接复制粘贴的工具函数

其实现在并不想写这种博客了,
毕竟工具类函数真的随便搜,加上现在各种AI工具让搜索效率大幅提升了,很容易就能搜到可用函数。
但看到写的很棒的还是想记录下,就当水了一篇吧23333

加参数

function setUrlParams(params,liveUrl) {
    
    
    const url = new URL(liveUrl);
    Object.entries(params).forEach(([key, value]) => {
    
    
        url.searchParams.set(key, value);
    });
    const newUrl = url.toString();
    return newUrl
}

let liveUrl = 'https://www.baidu.com/';
liveUrl = setUrlParams({
    
    autoplay:1},liveUrl)

提参数

function  getUrlRequest () {
    
    
  //var url = user_url; //获取url中"?"符后的字串
  const url = window && window.location.search; //获取url中"?"符后的字串
  const theRequest = new Object();
  const start = url.indexOf("?");
  if (start != -1) {
    
    
    let str = url.substring(start + 1);
    let strs = str.split("&");
    for (var i = 0; i < strs.length; i++) {
    
    
      theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
  }
  return theRequest;
};
const returnTo = getUrlRequest().returnTo;

猜你喜欢

转载自blog.csdn.net/Beatingworldline/article/details/140795246