HTML5 Browser Back button/Phone Back button event listener

Source: Internet
Author: User
Tags event listener

1.HTML5 History Object

Supports using the Pushstate () method to modify the address bar address without refreshing the page.

Popstate Events

The Popstate event will occur when the history entity is changed. Invoking the Pushstate () method or the Replacestate () method is triggered when the user clicks the browser return button.

Event Registration instance:

function (event) {    console.info (event);    Console.info (event.state);    Console.info (event.state.id);});

2. According to this event, if the trigger is to re-add the current page state, so that the user through the return button will not be able to leave the page.

This allows the return button to be locked in a state

<DivID= "Number"></Div><Script>pushhistory (); var Number=0; Window.addeventlistener ("popstate", function(e) {//alert ("I hear the browser's Return button event");//implement your own functions according to your needs Number++; varP=Document.createelement ('P'); P.innerhtml=Number ; document.getElementById (' Number'). appendchild (P); Pushhistory (); //note, called here, can the user stay on this page    }, false); functionpushhistory () {var State={title:"title", URL:"#"        }; Window.history.pushState (state,"title", "#"); }</Script>

More:

HTML5 History object, JavaScript modifies the address bar without refreshing the page

HTML5 History object, JavaScript modifies the address bar without refreshing the page (ii)

HTML5 geolocation API Geo-location finishing (ii)

HTML5 Browser Back button/Phone Back button event listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.