hashchange

Detecting Back Button/Hash Change in URL

大憨熊 提交于 2019-11-26 00:58:25
问题 I just set up my new homepage at http://ritter.vg. I\'m using jQuery, but very minimally. It loads all the pages using AJAX - I have it set up to allow bookmarking by detecting the hash in the URL. //general functions function getUrl(u) { return u + \'.html\'; } function loadURL(u) { $.get(getUrl(u), function(r){ $(\'#main\').html(r); } ); } //allows bookmarking var hash = new String(document.location).indexOf(\"#\"); if(hash > 0) { page = new String(document.location).substring(hash + 1); if

On - window.location.hash - Change?

筅森魡賤 提交于 2019-11-25 23:57:24
问题 I am using Ajax and hash for navigation. Is there a way to check if the window.location.hash changed like this? http://example.com/blah #123 to http://example.com/blah #456 It works if I check it when the document loads. But if I have #hash based navigation it doesn\'t work when I press the back button on the browser (so I jump from blah#456 to blah#123). It shows inside the address box, but I can\'t catch it with JavaScript. 回答1: The only way to really do this (and is how the

Change the URL in the browser without loading the new page using JavaScript

梦想的初衷 提交于 2019-11-25 22:04:16
问题 How would I have a JavaScript action that may have some effects on the current page but would also change the URL in the browser so if the user hits reload or bookmark the new URL is used? It would also be nice if the back button would reload the original URL. I am trying to record JavaScript state in the URL. 回答1: With HTML 5, use the history.pushState function. As an example: <script type="text/javascript"> var stateObj = { foo: "bar" }; function change_my_url() { history.pushState(stateObj

Detecting Back Button/Hash Change in URL

烈酒焚心 提交于 2019-11-25 20:55:27
I just set up my new homepage at http://ritter.vg . I'm using jQuery, but very minimally. It loads all the pages using AJAX - I have it set up to allow bookmarking by detecting the hash in the URL. //general functions function getUrl(u) { return u + '.html'; } function loadURL(u) { $.get(getUrl(u), function(r){ $('#main').html(r); } ); } //allows bookmarking var hash = new String(document.location).indexOf("#"); if(hash > 0) { page = new String(document.location).substring(hash + 1); if(page.length > 1) loadURL(page); else loadURL('news'); } else loadURL('news'); But I can't get the back and