Synchronize button on two different html pages using javascript

Viewed 44

I just want to know if there's a way that I can synchronize the map zoom button on two different html pages?

https://i.stack.imgur.com/nrFf9.png

I want to sync all the buttons in left and right map.

Here's the code for my Left and Right Map

LEFT

var scale = 1,
  panning = false,
  pointX = 0,
  pointY = 0,
  start = { x: 0, y: 0 },
  zoom1 = document.getElementById("zoom1");
  zoom2 = document.getElementById("zoom2");
  
function setTransform() {
  zoom1.style.transform = "translate(" + pointX + "px, " + pointY + "px) scale(" + scale + ")";
  zoom2.style.transform = "translate(" + pointX + "px, " + pointY + "px) scale(" + scale + ")";
}

function reset(){
   event.preventDefault();
   scale = 1;
   pointX = 0;
   pointY =  0;
   start = { x: 0, y: 0 };
   setTransform();
}
function zoomOut(){
   document.getElementById("zoomout").click();
   event.preventDefault();
   var xs = (event.clientX - pointX) / scale,
       ys = (event.clientY - pointY) / scale,
         delta = (event.wheelDelta ? event.wheelDelta : -event.deltaY);
         (delta > 0) ? (scale *= 1.04) : (scale /= 1.04);
   setTransform();
}
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>LEFT</title>
   <style>
    #vatican g path {
  -moz-transition-property: opacity;
  -o-transition-property: opacity;
  -webkit-transition-property: opacity;
  transition-property: opacity;
  -moz-transition-duration: 0.5s;
  -o-transition-duration: 0.5s;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -moz-transition-timing-function: ease;
  -o-transition-timing-function: ease;
  -webkit-transition-timing-function: ease;
  transition-timing-function: ease;
  stroke: gray;
  stroke-width: 0.5px;
}
   </style>
</head>
<body>
<div class="btn-zoom">
   <div>
      <button id="zoomout" onclick="zoomOut()">-</button>
   </div>
   <div>
      <button onclick="reset()">R</button>
   </div>
</div>
<div class="zoom_outer">
    <div id="zoom1">
<svg id="vatican"
    xmlns:mapsvg="http://mapsvg.com"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:cc="http://creativecommons.org/ns#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    version="1.1"
    height="100%"
    width="100%" 
    viewBox="0 300 2000 2100"  
    preserveAspectRatio="none"
    mapsvg:geoViewBox="116.927573 20.834769 126.606549 4.640292">
    <g>
        <path id="VA" title="Vatican City  (Holy See)" class="land" d="M473.758,317.509L486.073,391.484L578.644,421.415L578.099,480.789L575.735,515.828L573.327,572.038L573.872,588.098L583.461,593.693L591.868,602.697L601.457,614.133L605.684,621.92L610.455,639.683L612,659.392L611.728,665.231L611.546,670.827L609.592,681.777L602.32,697.106L587.551,713.652L573.872,721.195L566.237,723.871L553.922,724.602L539.471,721.195L527.064,715.599L521.384,709.759L515.612,704.649L500.025,709.272L480.983,716.572L448.536,728.981L417.452,736.037L406.092,740.417L402.683,745.526L403.047,752.583L403.683,767.182L409.908,773.508L410.408,781.294L407.455,788.837L402.683,792L393.186,785.674L347.014,767.912L346.56,778.618L340.379,778.131L266.94,767.182L241.219,762.802L235.993,752.583L223.541,747.474L200.683,744.797L191.685,740.417L179.733,737.983L165.963,736.037L150.194,736.523L150.785,748.203L126.335,753.313L109.249,755.016L104.931,750.637L98.75,743.094L103.931,737.983L109.703,732.874L94.479,718.762L78.21,705.379L66.803,697.106L48.625,681.777L40.582,692.727L17.224,679.101L0,672.287L55.351,638.952L75.574,625.326L79.709,622.406L86.344,610.97L91.525,593.693L95.433,584.204L102.522,575.201L112.521,566.928L124.018,554.762L134.47,546.488L140.741,536.998L144.877,524.102L149.648,494.902L154.011,471.299L161.146,466.188L171.598,461.809L184.095,455.969L199.274,441.368L212.089,422.875L221.224,398.541L227.858,380.047L273.575,374.208L298.343,375.667L305.932,374.208L339.788,361.554L353.694,357.173L360.783,369.097L390.322,360.337L388.913,345.493L409,339.166z"/>
    </g>
</svg>
</div>
</div>
<script src="main.js"></script>
</body>
</html>

RIGHT

var scale = 1,
  panning = false,
  pointX = 0,
  pointY = 0,
  start = { x: 0, y: 0 },
  zoom1 = document.getElementById("zoom1");
  zoom2 = document.getElementById("zoom2");
  
function setTransform() {
  zoom1.style.transform = "translate(" + pointX + "px, " + pointY + "px) scale(" + scale + ")";
  zoom2.style.transform = "translate(" + pointX + "px, " + pointY + "px) scale(" + scale + ")";
}

function reset(){
   event.preventDefault();
   scale = 1;
   pointX = 0;
   pointY =  0;
   start = { x: 0, y: 0 };
   setTransform();
}
function zoomOut(){
   document.getElementById("zoomout").click();
   event.preventDefault();
   var xs = (event.clientX - pointX) / scale,
       ys = (event.clientY - pointY) / scale,
         delta = (event.wheelDelta ? event.wheelDelta : -event.deltaY);
         (delta > 0) ? (scale *= 1.04) : (scale /= 1.04);
   setTransform();
}
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>RIGHT</title>
   <style>
    #vatican g path {
  -moz-transition-property: opacity;
  -o-transition-property: opacity;
  -webkit-transition-property: opacity;
  transition-property: opacity;
  -moz-transition-duration: 0.5s;
  -o-transition-duration: 0.5s;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -moz-transition-timing-function: ease;
  -o-transition-timing-function: ease;
  -webkit-transition-timing-function: ease;
  transition-timing-function: ease;
  stroke: gray;
  stroke-width: 0.5px;
}
   </style>
</head>
<body>
<div class="btn-zoom">
   <div>
      <button id="zoomout" onclick="zoomOut()">-</button>
   </div>
   <div>
      <button onclick="reset()">R</button>
   </div>
</div>
<div class="zoom_outer">
    <div id="zoom2">
<svg id="vatican"
    xmlns:mapsvg="http://mapsvg.com"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:cc="http://creativecommons.org/ns#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    version="1.1"
    height="100%"
    width="100%" 
    viewBox="0 300 2000 2100"  
    preserveAspectRatio="none"
    mapsvg:geoViewBox="116.927573 20.834769 126.606549 4.640292">
    <g>
        <path id="VA" title="Vatican City  (Holy See)" class="land" d="M473.758,317.509L486.073,391.484L578.644,421.415L578.099,480.789L575.735,515.828L573.327,572.038L573.872,588.098L583.461,593.693L591.868,602.697L601.457,614.133L605.684,621.92L610.455,639.683L612,659.392L611.728,665.231L611.546,670.827L609.592,681.777L602.32,697.106L587.551,713.652L573.872,721.195L566.237,723.871L553.922,724.602L539.471,721.195L527.064,715.599L521.384,709.759L515.612,704.649L500.025,709.272L480.983,716.572L448.536,728.981L417.452,736.037L406.092,740.417L402.683,745.526L403.047,752.583L403.683,767.182L409.908,773.508L410.408,781.294L407.455,788.837L402.683,792L393.186,785.674L347.014,767.912L346.56,778.618L340.379,778.131L266.94,767.182L241.219,762.802L235.993,752.583L223.541,747.474L200.683,744.797L191.685,740.417L179.733,737.983L165.963,736.037L150.194,736.523L150.785,748.203L126.335,753.313L109.249,755.016L104.931,750.637L98.75,743.094L103.931,737.983L109.703,732.874L94.479,718.762L78.21,705.379L66.803,697.106L48.625,681.777L40.582,692.727L17.224,679.101L0,672.287L55.351,638.952L75.574,625.326L79.709,622.406L86.344,610.97L91.525,593.693L95.433,584.204L102.522,575.201L112.521,566.928L124.018,554.762L134.47,546.488L140.741,536.998L144.877,524.102L149.648,494.902L154.011,471.299L161.146,466.188L171.598,461.809L184.095,455.969L199.274,441.368L212.089,422.875L221.224,398.541L227.858,380.047L273.575,374.208L298.343,375.667L305.932,374.208L339.788,361.554L353.694,357.173L360.783,369.097L390.322,360.337L388.913,345.493L409,339.166z"/>
    </g>
</svg>
</div>
</div>
<script src="main.js"></script>
</body>
</html>

I am calling the same JS file for the 2 pages.

This will help me alot if you guys can help me with this. Thank you!

1 Answers

Irrespective you use the same JS file for the two different page, both pages will load the JS as a new version for their own. so, even if you do some script change on one page, it wont reflect on the other until you refresh it.

Instead you can use Database to handle this as one of the solution.

  • Lets say on default both zoom was set at 8, and zoom button has a class "zoom_btn", and data-zoom='8'
  • Create a DB table called zoom_change with a column zoom_value with default one row with value of '8'.
  • Create a page in PHP or any other backend you use to update zoom_value in zoom_change table
  • Create another page in PHP or any other backend you use to fetch zootem_value in zoom_change table

using JQuery : on the common JS page :

Script #1 to Record zoom changes into DB:

$(".zoom_btn").on("click", function() {
   var zoom_value = parseInt($(this).attr("data-zoom"))+1;
   $.post('zoom_value_update.php', 
    {
      value : zoom_value
    }, function () {
       // $(this).attr("data-zoom",zoom_value);
       // Do something with the zoom value ----
   });
}); 

Script #2 to update zoom_value from DB to data attribute :

function() {
   setInterval(() => {
     $.post('zoom_value_fetch.php', 
      { }, function (result) {
       $(".zoom_btn").attr("data-zoom",result);
       // Do something with the zoom value ----
     });
   }, 5000);    
}();

Please note that this one of the method i know of. Not the only one. Please consider other answers as well.

Related