I'm pretty much a novice when it comes to js so I'm sorry if I'm missing something really simple.
Basically, I've done some research into using the history.pustate and popstate and I've made it so a query string is added to the end of the url (?v=images
) or (?v=profile
)...(v
meaning 'view') by using this:
var url = "?v=profile"
var stateObj = { path: url };
history.pushState(stateObj, "page 2", url);
I want to make it so I can load content into a div but without reloading the page which I have done using the .load()
function.
I then used this code:
$(window).bind('popstate', function(event) {
var state = event.originalEvent.state;
in $(document).ready()
section and later tried within just <script>
tags and neither worked.
I don't know how to make it so the content changes when I use the back button or at least makes it so I can trigger my own function to do so; and I'm assuming it has something to do with the state object?! I just can't seem to find anything online that explains the process clearly.
If someone could help me out it would be amazing and thank you in advance to anyone who does!
Maybe it's not best solution, and maybe it doesn't suit your needs. But for me it was best to just reload the page. So the page is consistent an it loads everything according to current querystring.
The
popstate
only contains a state when there is one.When it goes like this:
pushState
then there is no state, because the initial page was loaded regularly, not with
pushState
. As a result, theonpopstate
event is fired with astate
ofnull
. So when it isnull
, it means the original page should be loaded.You could implement it such that
history.pushState
will be called consistently and you only need to provide a state change function like this: Click here for jsFiddle link