somdow
somdow

Reputation: 6318

Javascript substring method assistance

So long story short im working on a web app and using AJAX within it. I'm trying to disable the default actions of links when clicked, attach a hash value to the link and then remove the "#" from the url.

the problem im having is that, although the hash values are being attached accordingly, the substring method isnt extracting the "#", it extracts the letter after it.....

here is my code. PS, i left my comments inthere so you get where im trying to go with this so i dont know....my logic or setup may be wrong....

$(document).ready(function(){
    
    //app vars
    var mainHash = "index";
    var menuBtn = $('.leftButton');
    //~~~~~~load the index page at first go.
    loadPage();
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~menu show/hide
    menuBtn.click( function(){
        $('#menu').toggleClass();   
    });
    
    
    //Menu items on click , disable link default actions.
    $('#menu a').click( hijackLinks );
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~functions for mobile index load AND hijacking app links to AJAX links.
    function loadPage(url){
        if( url == undefined){
            $('#contentHere').load('index.html #content', hijackLinks);
                window.location.hash = mainHash;
        } else {
            $('#contentHere').load(url + '#content', hijackLinks ); 
        }
    }
    
    
    function hijackLinks(e){
        var url = e.target.href;
        e.preventDefault();
        loadPage(e.target.href);
        window.location.hash = $(this).attr("href").substring(1);
    }
});

what im wanting is to remove the "#" from the url. What am i doing wrong, what am i not seeing/understanding?

ive tried substring/substr etc and both do the same thing in that no matter what numbers i choose to insert into the substrings params, they remove EVERYTHING BUT the "#" lol....

Upvotes: 2

Views: 139

Answers (1)

Cokegod
Cokegod

Reputation: 8424

Well, you don't really change the link itself, you only change the window.location.hash, and the hash always has a "#" at the beginning.

What you need to do in order to change the entire url (and remove the '#') is to manipulate the browser history.

Although you should know it works only in newer browsers (the exact browser versions are in the link), so if you target your website to older too browsers you might need to think about having a fallback using the hash. If you decide to have such a fallback, I suggest searching for a plugin which does it instead of making it all yourself.

Upvotes: 1

Related Questions