Created
June 28, 2016 14:16
-
-
Save Nils-van-Kleef/53d290b48071fed217ba486462f3a158 to your computer and use it in GitHub Desktop.
Redirect - Remove query parameter for angular site. This JavaScript will redirect a visitor to the same URL with a query parameter removed, preserving the rest of the URL. For non-angular, change the line `window.location ..` to `window.location.replace(_optly.redir.href)`
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
/* _optimizely_redirect=http://www.example.com */ | |
/* remove Query Parameter from url */ | |
_optly = {redir: document.createElement("a")}; | |
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://", | |
domain: window.location.hostname, | |
pathname: window.location.pathname | |
}; | |
queryToRemove = "TO_REPLACE"; | |
_optly_cur_loc = window.location.href; | |
queryToRemoveIndexInHash = window.location.hash.indexOf(queryToRemove); | |
if (queryToRemoveIndexInHash > -1) { | |
if (window.location.hash.length == queryToRemoveIndexInHash+queryToRemove.length) { // queryToRemove at end of URL | |
_optly.redir.hash = window.location.hash.substring(0,queryToRemoveIndexInHash-1); | |
} else { // queryToRemove no at end of URL | |
_optly.redir.hash = window.location.hash.substring(0,queryToRemoveIndexInHash) + window.location.hash.substring(queryToRemoveIndexInHash+queryToRemove.length+1,window.location.hash.length); | |
} | |
_optly.redir.href = _optly.redir.protocol + _optly.redir.domain + _optly.redir.pathname + _optly.redir.hash + "?" + newQuery; | |
window.location = _optly.redir.href; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment