Skip to content

Instantly share code, notes, and snippets.

@kieranstartup
Created October 8, 2017 20:22
Show Gist options
  • Save kieranstartup/9b5de2b65944317ff993bc4e32455114 to your computer and use it in GitHub Desktop.
Save kieranstartup/9b5de2b65944317ff993bc4e32455114 to your computer and use it in GitHub Desktop.
// This is useful for dumb single page WordPress webapps which contain multiple Custom Post Types on a single page and need to link
// i.e. smooth scroll to those sections on page load as you can't share URL's with hashes in them
var pathnameMatch;
var urlPathname = window.location.href;
var sanitized = urlPathname
.replace(/^http\:\/\//, '') // remove the leading http:// (temporarily)
.replace(/\/+/g, '/') // replace consecutive slashes with a single slash
.replace(/\/+$/, ''); // remove trailing slashes
urlPathname = 'http://' + sanitized;
if (urlPathname.includes("news")) {
pathnameMatch = true;
}
if (pathnameMatch) {
var beforeHashURL = urlPathname;
var addHashURL = beforeHashURL.split("/").pop();
if (urlPathname.includes("#")) {
var hashURLToAdd = addHashURL;
window.location.replace('../' + hashURLToAdd);
} else {
var hashURLToAdd = "#" + addHashURL;
window.location.replace('../' + hashURLToAdd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment