Cross-page listening in the same domain

Source: Internet
Author: User

Open several pages in the same browser.cookieTherefore, assume that the page for selecting a song is a page, and the page for music box is B page. When you click the play button on page A, write a cookie and set a timer on page B, continue to check whether the cookie has changed, and then proceed with further processing.

UsecookieIt's still quite troublesome. Another thing that will also be shared isStorageAnd it hasonstorageMethod listeningStorageThat is much more convenient. Write a simple example. The target is to click the button on page A. If page B is not opened, page B is opened and data is accepted. If page B is already opened, data is accepted directly. BelowPage BAcceptPageUploadedIDThe value isNew, UseIsloginJudgmentPage BWhether it is enabled.

A.html

VaR id = 0; // For convenience, use a variable to record var flag = false; BTN. onclick = function () {ID ++; additem () ;}function additem () {var islogin = localstorage. getitem ('islogin'); If (islogin) {localstorage. setitem ('new', ID);} else {window. open ('. /B .html '); flag = true;} window. addeventlistener ('store', function (e) {If (flag & E. key = 'islogin') {flag = false; additem () ;}, false );


B .html


// Add the record localstorage when it is enabled. setitem ('islogin', 1); window. onbeforeunload = function () {// if it is disabled, the record is deleted. Otherwise, localstorage is unavailable. removeitem ('islogin');} window. addeventlistener ("Storage", function (e) {If (E. key = 'new') {console. log (E. newvalue) ;}}, false );
 

Cross-page listening in the same domain

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.