samedi 18 février 2017

Using .on("hashchange", ...) to trigger page changes in a single page application?

I was wondering if it is bad practice to use something like

window.location.hash = '#example';

and having this:

$(window).on('hashchange', function() {
    if (window.location.hash) {
        navigateToHashUrl(window.location.hash);
    }
});

function navigateToHashUrl(hashUrl) {
    if(hashUrl == '#example') {
        goToExamplePage();
    } else if ...
    ...
}

to delegate to the actual methods that will use $().load('html') to load the page?

In some cases in my web-app, the front end will send a request, and if everything is OK, the back end will respond with data that indicates to change the hash url. In those cases, I use this kind of thing.

I don't see why it would be bad practice, but I am new to web-dev and would like to know if I am doing anything that I shouldn't be doing.

Thanks!




Aucun commentaire:

Enregistrer un commentaire