Last active
September 7, 2016 11:20
-
-
Save Nils-van-Kleef/77c7c9bb4ff1b91e9446692646d7546b to your computer and use it in GitHub Desktop.
Redirect - Keep string. This JavaScript will redirect a visitor to a different URL but keeps everything up until a defined part of the URL.
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 */ | |
var toKeep = "/[TO_KEEP]"; | |
var addition = "/[NEW_ADDITION]"; | |
var _optly = {redir: document.createElement("a")}; | |
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://", | |
domain: window.location.hostname, | |
first: window.location.href.split(toKeep)[0], | |
query: window.location.search | |
}; | |
_optly.redir.href = _optly.redir.first + toKeep + addition + _optly.redir.query; | |
window.location.replace(_optly.redir.href); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment