简体   繁体   中英

JavaScript: How to get first parameter, actually first key from URL?

I am trying to get first key from URL, like this:

const currentUrl = window.location.href; 
const parmsFromUrl = new URLSearchParams(currentUrl);
const paramToCheck = parmsFromUrl.keys[0];

But constantly I get undefined, can someone tell how me how to get only first parameter, not a value, I just want key? Thanks.

With URLSearchParams you have to use window.location.search

With URL you have to use window.location.href

const currentUrl = window.location.search;
const parmsFromUrl = new URLSearchParams(currentUrl);

URLSearchParams.keys() returns an iterator, so you have to use next() to get first value

const paramToCheck = parmsFromUrl.keys().next().value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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