I have this simple string:
var url_string = 'https://www.booking.com/searchresults.html?label=gen173nr-1DCAEoggJCAlhYSDNYBGjBAYgBAZgBMcIBCndpbmRvd3MgMTDIAQzYAQPoAQH4AQKSAgF5qAID;sid=2a2d61dc6122c8a17b369733693bc965;checkin_month=5&checkin_monthday=17&checkin_year=2018&checkout_month=6&checkout_monthday=1&checkout_year=2018&class_interval=1&dest_id=-90115&dest_type=city&dtdisc=0&from_sf=1&group_adults=3&group_children=0&inac=0&index_postcard=0&label_click=undef&no_rooms=1&offset=0&postcard=0&raw_dest_type=city&room1=A%2CA%2CA&sb_price_type=total&src=index&src_elem=sb&ss=Ni%C5%A1&ss_all=0&ssb=empty&sshis=0&ssne=Ni%C5%A1&ssne_untouched=Ni%C5%A1&'
which I got from
var url_string = window.location.href;
var url = new URL(url_string);
that is an address that currently I am on. And now I want to extract some parameters
var no_rooms = url.searchParams.get("no_rooms"); // 1
var group_children = url.searchParams.get("group_children"); // 0
var checkin_month = url.searchParams.get("checkin_month"); // NaN
How is it possible that checkin_month
, a parameter that exists in a string, and it is a number, prints NaN
? What am I missing here?
EDIT: I replaced the ;
in a string with ?
using
url_string.replace(/;/g, "?");
And repeated the same process, and when I got to the
var checkin_month = url.searchParams.get("checkin_month"); // I got null
EDIT2: I was replacing with ?
when I should replace with &
.
As the url is not formatted, so two way to go.
Solution 1:
Replace ;
with &
to make it query parameter.
var url_string = 'https://www.booking.com/searchresults.html?label=gen173nr-1DCAEoggJCAlhYSDNYBGjBAYgBAZgBMcIBCndpbmRvd3MgMTDIAQzYAQPoAQH4AQKSAgF5qAID;sid=2a2d61dc6122c8a17b369733693bc965;checkin_month=5&checkin_monthday=17&checkin_year=2018&checkout_month=6&checkout_monthday=1&checkout_year=2018&class_interval=1&dest_id=-90115&dest_type=city&dtdisc=0&from_sf=1&group_adults=3&group_children=0&inac=0&index_postcard=0&label_click=undef&no_rooms=1&offset=0&postcard=0&raw_dest_type=city&room1=A%2CA%2CA&sb_price_type=total&src=index&src_elem=sb&ss=Ni%C5%A1&ss_all=0&ssb=empty&sshis=0&ssne=Ni%C5%A1&ssne_untouched=Ni%C5%A1&' var newUrlString = url_string.replace(/;/g, '&'); var url = new URL(newUrlString); var checkin_month = url.searchParams.get("checkin_month"); console.log("checkin_month is " + checkin_month);
Solution 2: Parse the string from your existing URL object
var url_string = 'https://www.booking.com/searchresults.html?label=gen173nr-1DCAEoggJCAlhYSDNYBGjBAYgBAZgBMcIBCndpbmRvd3MgMTDIAQzYAQPoAQH4AQKSAgF5qAID;sid=2a2d61dc6122c8a17b369733693bc965;checkin_month=5&checkin_monthday=17&checkin_year=2018&checkout_month=6&checkout_monthday=1&checkout_year=2018&class_interval=1&dest_id=-90115&dest_type=city&dtdisc=0&from_sf=1&group_adults=3&group_children=0&inac=0&index_postcard=0&label_click=undef&no_rooms=1&offset=0&postcard=0&raw_dest_type=city&room1=A%2CA%2CA&sb_price_type=total&src=index&src_elem=sb&ss=Ni%C5%A1&ss_all=0&ssb=empty&sshis=0&ssne=Ni%C5%A1&ssne_untouched=Ni%C5%A1&' var url = new URL(url_string); var checkin_month = url.searchParams.get('label').split(';').find(e => e.includes('checkin_month')).split('=')[1] console.log("checkin_month is " + checkin_month)
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.