0

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);

1

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.