JS获取最终表单上的所有URL参数

发布于 2025-02-10 12:28:12 字数 100 浏览 2 评论 0原文

我看到很多方法,其中许多似乎过于复杂。 我确实计划附加一个程序,该程序将使用这些参数根据结果进行测试。

但是目前,我想要一种获取所有URL参数并使用AP标签发布它们的方法。

I see a lot of ways and a number of them seem overly complicated.
i do plan on appending a program that will use these parameters to test based on the results.

But for now, I want a way to get all the URL parameters and post them using a p tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2025-02-17 12:28:12

这会回答您的问题吗?您可以使用url构造函数获得URL的搜索参数,并访问searchParams属性。

const div = document.querySelector('div');

const appendParams = (url) => {
    const params = Object.fromEntries(new URL(url).searchParams.entries());

    for (const key in params) {
        const p = document.createElement('p');
        p.textContent = `${key}: ${params[key]}`;
        div.appendChild(p);
    }
};

window.addEventListener('DOMContentLoaded', () => {
    appendParams('https://hello.world?foo=bar&fizz=buzz&abc=123&hello=world');
});
<div></div>

Does this answer your question? You can get the search params of a URL with the URL constructor and accessing the searchParams property.

const div = document.querySelector('div');

const appendParams = (url) => {
    const params = Object.fromEntries(new URL(url).searchParams.entries());

    for (const key in params) {
        const p = document.createElement('p');
        p.textContent = `${key}: ${params[key]}`;
        div.appendChild(p);
    }
};

window.addEventListener('DOMContentLoaded', () => {
    appendParams('https://hello.world?foo=bar&fizz=buzz&abc=123&hello=world');
});
<div></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文