JQuery.address how to remove # from url (javascript window.location remove #)

12,576

Solution 1

try this

newHash = window.location.hash.substring(1);

console.log(newHash);

if you have an anchor then its usually appended at the beginning of the URL so but removing the first character then, you shouldn't have it as an issue anymore.

or if you can even do it this way which i think is what you might need

newHash = window.location.hash.replace('#', '');

console.log(newHash);

Solution 2

Using window.location.hash = '' should remove it outright, otherwise there are some good plugins that open up a lot of functions for working with URLs (e.g. http://ajaxcssblog.com/jquery/url-read-request-variables).

Solution 3

You could also use this (which totally removes the hash and cleans the URL right back up)

history.pushState("", document.title, window.location.pathname);

Which changes

http://www.google.com/#top

to

http://www.google.com/

Solution 4

I've found that changing the value after the hash worked best for a scenario similar to yours. It would need to be a hash that doesn't already exist on your page.

function removeDeepLink(event) {
    window.location.hash = 'foo';
};
Share:
12,576
Admin
Author by

Admin

Updated on July 26, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to remove the # from url when my event.value is == to /. I've got a lighbox with jquery.address that stores references to open images, when i close it i need to remove the # mark becouse this cause window scroll top.

    I succed remove the # mark with this: window.location.href.slice(0, -1); but as you can see in the code above this cause the url rewrite when page is loaded and not only after my event.

    How can i chain this javascript only when my complete happens, in the way this function is called olny when i close the lightbox.

    I attach here my code with comments. thank you all

    $.address.change(function(event) {
        curLink = event.value;
        if(curLink != '/') {
    
        // here my stuff, jquery.address generates url with reference 
    
        // ex: mysite.com/cat/subcat/page.html#one
    
        } else {  
    
            $('#element').animate({opacity:"0"},{duration:100, easing:"quartEaseOut", complete: function () {
    
            // here I need to remove the hash only after the complete 
    
            // ex: mysite.com/cat/subcat/page.html#  >  mysite.com/cat/subcat/page.html
    
            window.location.href.slice(0, -1);           
            $(this).hide(); 
        }});   
      }   
    });