jquery 从windows获取数据,onload事件?

xu3bshqb  于 2023-01-25  发布在  jQuery
关注(0)|答案(1)|浏览(128)

我有一个onload函数来从当前URL中分割数据。

var url = document.location.href,
        params = url.split('?')[1].split('&'),
        data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
         tmp = params[i].split('=');
         data[tmp[0]] = tmp[1];
    }
    document.getElementById('here').innerHTML = data.name;
}

我需要将windows.onload事件中的拆分值用于函数

var te='https://myurl.html?AID=' + data; //need to use the split value here
qyswt5oh

qyswt5oh1#

像这样?

const url = new URL(document.location.href);
window.addEventListener('DOMContentLoaded', () => { // page load
  document.getElementById('here').innerHTML = url.searchParams.get('name'); // gets the &name=whatever
  const te = `https://myurl.html?AID=${url.searchParams.get('AID')}`; // gets the AID=whatever
})

相关问题