简体   繁体   English

如何仅使用javascript在另一个页面中显示链接参数详细信息?

[英]how to show a link parameter details in another page using javascript only?

我正在搜索并没有发现任何内容,我有一个页面规范.html,其中包含三个链接,例如 details.html?id=1 和 details.html?id=2 和 details.html?id=3 ,我想显示详细信息每个链接,如果有人点击了 details.html?id=1 链接,然后在 details.html 页面上显示 id =1 ,我可以用 php 来做,但我的老师说我只能用 javascript 来做,我不知道该怎么做,所以请如果有人帮助我,我将非常感谢你

As far as I get it, you want to know the value of the "id" search param when accessing the "details.html" page.据我所知,您想在访问“details.html”页面时知道“id”搜索参数的值。 As stated in this answer to the question "How can I get query string values in JavaScript?"如对问题“如何在 JavaScript 中获取查询字符串值?”的回答中所述 , you can use URLSearchParams which is simple and has good browser support . ,您可以使用 URLSearchParams,它简单且具有良好的浏览器支持

const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id'); // for a search "&id=2", the value of "id" will be 2.

This will create an URLSearchParams object which parses the window.location.search string and is able to return one specific param, like "id".这将创建一个 URLSearchParams 对象,该对象解析window.location.search字符串并能够返回一个特定的参数,如“id”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM