Reputation: 6509
I want to visit https://example.com/?GCLID=test123 and store whatever is in GCLID in a variable.
How do I do this? The following keeps returning null
var url = window.location.href;
// test
url = "https://example.com/?GCLID=test123";
const params = new URLSearchParams(url);
var gclid = params.get('GCLID');
alert(params);
alert(gclid);
Upvotes: 0
Views: 344
Reputation: 1503
You have to take the part after '?' in new URLSearchParams, see below example for same, i.e you will pass window.location.search like this
const params = new URLSearchParams(window.location.search);
var url = window.location.href;
// test
url = "https://example.com/?GCLID=test123";
const params = new URLSearchParams(url.split('?')[1]);
var gclid = params.get('GCLID');
alert(params);
alert(gclid);
Upvotes: 1