I'm trying to create an SPA website with JQuery. I know how to use history.pushState
and $(window).on('popstate')
but i have a few questions. Currently i have a router.js file which handles the popstate event and the navigation links. I place the links in a custom attribute(data-navId="templates/page/page.html") or in href and handling it with
$(e).preventDefault();
. User clicks the link and $('.page-content').load('url', function() {})
and history.pushstate
are executed.
1) Am i forced to include the router.js file containing these functions in every template? I want one html which contains these functions only. The templates should only contain HTML and their corresponding scripts. I managed to accomplished that but if the user refreshes the page the template shows without styling or anything. Is using a hash the answer? Or if the user closes the browser and have the "Restore previous pages" option activated the template will be load with its html only.
In the past i developed SPA pages without hash using only popstate
and history.pushState
but i had to include all the html on each file and then calling $('.page-content').load(url + ' .page-content > *', function() {});
to avoid the refresh bug.
I also know that facebook uses popstate
for modern browsers and hashchange
for older perhaps by including Modernizr.js or some custom library. But how the router works without including it in every template?
Thanks in advance.