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 usewindow.location.search
With
URL
you have to usewindow.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.