简体   繁体   中英

Remove hash sign and all the text after it in url

How do I remove a URL's hash sign and the text after it?

For example, the URL is http://www.website.com/home#content

I want the whole #content text to be removed.

window.location.href.split('#')[0]  

你可以试试!

I think this is what you're looking for... (ie when you click a hyperlink that has a hash in its href, you want to strip out the hash and navigate to the remaining URL?)

<!DOCTYPE html>
<html>
    <body>
        <a id="someLink" href="/some_page#some_hash">Click Me</a>
        <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
        <script>
            $('#someLink').click(function (e) {
                // Prevent normal navigation to the href (the full URL with hash)
                e.preventDefault();
                // Navigate to "/some_page" (everything to left of the first "#")
                document.location = this.href.split('#')[0];
            });
        </script>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM