Created
October 27, 2022 09:31
-
-
Save showgo001/b6ba5d46332f7da3b25cfc0c057d2160 to your computer and use it in GitHub Desktop.
特定のURLへの遷移リンクにGETパラメータを引き継ぐJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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