0

I'm trying to write a small web application that is able to take in data from URL parameters and display appropriate information about that data. The page is not rendered server side so all the parameters do is tell that javascript what to send to my JSON API and load into the view. The page is being loaded from another, desktop application, which encodes the data to use in the URL it loads since that's the only way I can get it to communicate with the site... so, for example, the desktop app would generate a link like

http://myapp/?room=1&tile=7

This works fine for the first instance, but I would like it to work like many desktop applications do where if you load the app another time, it just brings up the original instance with the new data. I know that the new page will have to at least briefly load, but is there a way of having it just take the new data, send it to the first instance of the page and then close itself? I know sort of how to do this when I have a main page and I use window.open() to open a second, which I can force to reload. But I've never done it where the first instance is opened manually and I then need to interact with that.

Adam Haile
  • 30,705
  • 58
  • 191
  • 286
  • Have you thought about using localStorage to handle this? You could load the page, save data using localStorage and expect it on the original page. But you still have to manage the detection of wether or not the original instance still exist. – E. Sundin Dec 31 '14 at 13:58
  • Yeah, that's what I'm going to do... the detection and window closing is my problem. – Adam Haile Dec 31 '14 at 14:06
  • 1
    It sounds like you're making this way more complicated than it needs to be. – Etheryte Dec 31 '14 at 14:16
  • Ok... then what's a simpler solution? – Adam Haile Dec 31 '14 at 14:51

1 Answers1

0

A major problem with this implementation is that window.close() will not work so you have to ask the user to close the window. The "problem" or feature is talked about here https://stackoverflow.com/a/19768082/3951400

<!doctype html>
<html>
<head>
<script>
function update() {
  localStorage.setItem('lastUpdate', Date.now());
  // Here you could check the data-item for changes
}

function setData() {
  localStorage.setItem('data', window.location.search);
}

var t, lastUpdate = localStorage.getItem('lastUpdate');
if(lastUpdate === null || (Date.now() - Number(lastUpdate) > 1000)) {
  // Likely no other window
  setData();
  t=setInterval(update, 100);
} else {
  setData();
  alert("please close this. ctrl+w");
}
</script>
</head>
<body>
Hello
</body>
</html>
Community
  • 1
  • 1
E. Sundin
  • 4,103
  • 21
  • 30