简体   繁体   中英

How to handle browser refresh when using dojo/hash?

On a single page app, my problem is I'm subscribing to the hashchange event to render the content (create and destroy widgets that represent my pages), but the function only gets fired when the hash actually changes, such as when the browser back and forward buttons are clicked.

My main javascript file that always gets loaded on first time and also on refresh contain the following

topic.subscribe("/dojo/hashchange", function(newhash){
    //set content based on hash
});

When refresh is clicked, it doesn't get fired and I'm left with a blank page (all my logic to render the page lies inside the anonymous function for that topic I subscribed to)

your subscribe isn't being called on a refresh as the hash didn't actually change. you're subscribing to hash changes, changes that happen after the page as loaded.

using this subscribe method things can get out of hand quickly. you may want to look at using route. some links -

https://developer.mozilla.org/en-US/docs/Web/Events/hashchange

https://dojotoolkit.org/documentation/tutorials/1.9/hash/

https://dojotoolkit.org/reference-guide/1.10/dojo/router.html

https://www.sitepen.com/blog/2014/06/18/dojo-faq-does-dojo-have-routing-like-backbone-and-embe/

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