Skip to content

Instantly share code, notes, and snippets.

@Nils-van-Kleef
Last active September 7, 2016 11:20
Show Gist options
  • Save Nils-van-Kleef/77c7c9bb4ff1b91e9446692646d7546b to your computer and use it in GitHub Desktop.
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.
/* _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