0

Thanks for taking time to improve my code. I have a Google Ads link which when clicked contain "Gclid" at the end. URL: https://google.com/?gclid=RandomGclidHere.

My Javascript Code is,

document.getElementById("Australia").onclick = function () {
    location.href = "https://google.com/au.php?gclid=ENTERGCLIDHERE";
};
document.getElementById("New Zealand").onclick = function () {
    location.href = "https://google.com/nz.php?gclid=ENTERGCLIDHERE";
};
document.getElementById("United States").onclick = function () {
    location.href = "https://google.com/us.php?gclid=ENTERGCLIDHERE";
};

I have a drop down on the page which contain 3 options, Australia, New Zealand & USA. The visitor is redirected based on drop-down selection. I want to get the "Gclid" from URL and post the "Gclid" to the URLS through Javascript.

Please guide!

1
  • Does this answer help you? Commented Sep 3, 2022 at 7:03

1 Answer 1

1

If by dropdown you mean a select, I would suggest putting the event listener on the select itself, and using its value:

document.querySelector('select').addEventListener('change', handleCountryChange)

function handleCountryChange(event) {
const countrySelected = event.target.value
const countryDict = { 'australia': 'au', 'country': 'code'}
const countryCode = countryDict[countrySelected]
const gclid = 'ENTERGCLIDHERE'
const googleUrl = `https://google.com/${countryDict}.php?gclid=${gclid}`

window.location.href = googleUrl
}

Having less event listeners is usually desired.

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

1 Comment

Hi Larsw, 1. I want to grab the parameter from URL: '?gclid=RandomGclidHere.' 2. When the Visitor select the Country Name 'AU, NZ or US', I want to add the grabbed parameter (in this case google click id) and post at the end of the URL in place of 'ENTERGCLIDHERE' , google.com/us.php?gclid=ENTERGCLIDHERE.

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.