Created
October 8, 2017 20:22
-
-
Save kieranstartup/9b5de2b65944317ff993bc4e32455114 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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