问题
I need help with syntax.
My site loads blog posts within the #board div using AJAX and I close it by clicking #close. When I load a post the url becomes like this http://www.visualise.ca/#!/anne-au-cherry and I would like to come back to http://www.visualise.ca/ when I close the post. The following gives me http://www.visualise.ca/#/
$("#close").live("click", function(event) {
$("#board").slideUp("slow");
window.location.hash = "#/";
window.history.pushState(null,null,site_url+"/");
return false;
});
1) Can someone help please ?
2) What if the browser doesn't support html5 ?
Many thanks for your time and help.
UPDATE: THIS WORKS, there was a typo in my 'site_url' variable.
回答1:
PushState is not operation over the hash. If you want it to be < html5 compatible you need to use hash.
pushState is changing the url without changing page:
If you see the history as an array, history = [];
You open your browser's empty frontpage and go to page1.html
now history is = ['page1.html']
.
If you fires pushState from page1.html with the url page2.html the history is now ['page1.html','page2.html']
and the address bar shows page2.html.
If the browser dosn't support pushState it does nothing. So for your example:
$("#close").live("click", function(event) {
$("#board").slideUp("slow");
window.history.pushState(null, null, site_url+"/");
return false;
});
And when you load your ajax:
window.history.pushState(null,null,site_url + "/" + ajax_url);
If you want to operate with hash you can do something like this:
$("#close").live("click", function(event) {
$("#board").slideUp("slow");
window.location.href = "#/"
return false;
});
And when you load your ajax:
window.location.href = "#/" + ajax_url
If you are using pushState be aware of the urls can end op pointing in subfolders you dont have and therefore you need some kind of .htaccess rewrite code
来源:https://stackoverflow.com/questions/7008021/help-with-window-history-pushstate