javascript - How to trap the back button and refresh the page using ajax -


i have web page updates progressivly therefore need windows "back" button call function refresh data , not go page. have first part working trap "back" button with

window.onbeforeunload = confirmexit(); 

and confirmexit()

function confirmexit() {     switch (page) {         case 2:             countries();             break;          case 3:             counties(page3id)             break;         case 4:             cities(page4id)             break;     } } 

which works after the page refreshed button default action kicks in , loads previous page. have tried returning true , false. appreciate help, thank you.

you need add virtual sites browsers history. way button not lead website.

use history.pushstate() add virtual sites.

suppose http://mozilla.org/foo.html executes following javascript:

var stateobj = { foo: "bar" };  history.pushstate(stateobj, "page 2", "bar.html"); 

this cause url bar display http://mozilla.org/bar.html, won't cause browser load bar.html or check bar.html exists.

suppose user navigates http://google.com, clicks back. @ point, url bar display http://mozilla.org/bar.html, , page popstate event state object contains copy of stateobj. page foo.html, although page might modify contents during popstate event.

if click again, url change http://mozilla.org/foo.html, , document popstate event, time null state object. here too, going doesn't change document's contents in previous step, although document might update contents manually upon receiving popstate event.

from mdn: manipulating browser history

the user can press button number of times added virtual sites. detect button press use window.onpopstate

the popstate event

a popstate event dispatched window every time active history entry changes. if history entry being activated created call pushstate or affected call replacestate, popstate event's state property contains copy of history entry's state object.

from mdn: manipulating browser history


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -