How do i modify this to return the current page without the query string document.write(location.href.split("/").slice(-1)); Code (markup):
Try this, but this gets full URL, not just filename like your original function does. <script type="text/javascript"> document.write( location.href.split("?").slice(0,1) ); </script>
use .pathName insteaad of .href. I'd also suggest using substring instead of split/slice as it executes faster. location.pathName.substring(location.pathName.lastIndexOf('/') + 1); Code (markup): Should do the trick. You could also get the length to remove from the end of location.href via location.search, but that could still include the hash so you'd need that as well. Remember, window.location is an instance of Location. Hence all the properties of Location are available to you: https://developer.mozilla.org/en-US/docs/Web/API/Location It's already split into pathname, hash, search (query string), etc, etc. Location.pathName is also nice in that it already has the protocol stripped off.
thanks for replies, I went with this: document.write(location.pathname.split("/").slice(-1)); Code (markup):