Skip to content

Instantly share code, notes, and snippets.

@showgo001
Created October 27, 2022 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save showgo001/b6ba5d46332f7da3b25cfc0c057d2160 to your computer and use it in GitHub Desktop.
Save showgo001/b6ba5d46332f7da3b25cfc0c057d2160 to your computer and use it in GitHub Desktop.
特定のURLへの遷移リンクにGETパラメータを引き継ぐJS
window.onload = function setCvQuearyOnLoad() {
var param = GetQueryString();
var target_param = "key";
var target_val = "";
var target_url = "example.com";
if (param != null && param[target_param] != undefined) {
target_val = param[target_param];
var ankers = document.getElementsByTagName('a');
for (i = 0; i < ankers.length; i++) {
var href = ankers[i].href;
if (href.indexOf(target_url) === 0) {
var glue = href.indexOf('?') === -1 ? '?' : '&';
ankers[i].href = href + glue + target_param + '=' + target_val;
}
}
}
}
function GetQueryString() {
if (1 < document.location.search.length) {
var query = document.location.search.substring(1);
var parameters = query.split('&');
var result = new Object();
for (var i = 0; i < parameters.length; i++) {
var element = parameters[i].split('=');
var paramName = decodeURIComponent(element[0]);
var paramValue = decodeURIComponent(element[1]);
result[paramName] = decodeURIComponent(paramValue);
}
return result;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment