Change content of a div on another page

前端 未结 4 624
温柔的废话
温柔的废话 2021-01-24 06:52

On page 1, I have a div containing information.

And on page 2, I have a form

4条回答
  •  滥情空心
    2021-01-24 06:59

    One way is to do like what the other answers mentioned, to have each tab communicate to a central server that will get/send data to keep both tabs updated using AJAX for example.

    But I'm here to tell you about another way though, it's to use what we already have designed for this kind of task exactly. What so called browser localStorage

    Browser storage works like this pseudo code:

      //set the value, it works as a hash map or assoc array. 
       localStorage .setItem("some_index_key", "some data") ; 
       // get the value by it's index key. 
       localStorage .getItem("some_index_key") ; // will get you "some data" 
    

    Where all the data will be shared among all open tabs for the same domain. And you can add event listener so whenever one value change, it will be reflected on all tabs.

    addEvent(window, 'storage', function (event) {
      if (event.key == 'some_index_key') {
        output.innerHTML = event.newValue;
      }
    });
    
    addEvent(myInputField, 'keyup', function () {
      localStorage.setItem('some_index_key', this.value);
    });
    

    Check out this DEMO, you edit one field on page-A, and that value will be reflected on page-B offline without the need to burden the network.

    To learn more, read this.

    Real live example. The background color is controlled from another tab.

         var screenone = document.getElementById('screenone');
    
                screenone.addEventListener('keydown', screenOneFunction);
                screenone.addEventListener('change', screenOneFunction);
               
        function screenOneFunction()
                {
                    document.body.style.backgroundColor = this.value;
                    localStorage.setItem("color1", this.value);
                }
                
                
                
                var screentwo = document.getElementById('screentwo');
    
                screentwo.addEventListener('keydown', function (evt) {
                    localStorage.setItem("color2", this.value);
                });
                screentwo.addEventListener('change', function (evt) {
                    localStorage.setItem("color2", this.value);
                });
    
    
    
                var thebutton = document.getElementById('thebutton');
    
                thebutton.addEventListener('click', function (evt) {
                    localStorage.clear();
                    screenone.value = "";
                    screentwo.value = "";
                    document.body.style.backgroundColor = "";
                });
    
    
    
                var storageHandler = function () {
                    document.body.style.backgroundColor = localStorage.color2;
                    var color1 = localStorage.color1;
                    var color2 = localStorage.color2;
                    screenone.value = color2;
                    screentwo.value = color1;
    
                };
                window.addEventListener("storage", storageHandler, false);
           .screenone{ border: 1px solid black;}
           input{ margin: 10px; width: 250px; height: 20px; border:round}
          label{margin: 15px;}
    
             
        
        
            
            


提交回复
热议问题