从URL获取参数并在单击时用Javascript发布



感谢您花时间改进我的代码。我有一个谷歌广告链接,点击后包含";Gclid";最后。网址:https://google.com/?gclid=RandomGclidHere.

我的Javascript代码是,

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";
};

我在页面上有一个下拉列表,其中包含3个选项,澳大利亚、新西兰和澳大利亚;美国。根据下拉选择重定向访问者。我想得到";Gclid";从URL并发布";Gclid";通过Javascript连接到URLS。

请引导!

如果下拉列表是指选择,我建议将事件侦听器放在选择本身上,并使用其值:

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
}

通常需要较少的事件侦听器。

最新更新