New List Subject but related to Eran’s note...

The approach in this note is motivated by Eran’s questions, but it’s also 
motivated by a number of frame page communication issues that have come up as 
folks move older pages to JSmol.

It’s quick and dirty, but it works (No parent.top… or should it have been 
parent.top.top… or maybe just parent…).
I have not tried it on older frameset pages, but it should work.

The code is fairly simple:

if (window.addEventListener) { window.addEventListener("storage", talkToFriend, 
false); } // false ensures event bubbling - not capture
if (window.attachEvent) { window.attachEvent("storage", talkToFriend, false); } 
// keep i.e. happy - sigh
function talkToFriend(e) {
        if (localStorage.getItem('talkToFriend')) {
                var data = localStorage.getItem('talkToFriend');
                localStorage.removeItem('talkToFriend’); // sending page will 
get this message, but above “if" will kill action. Classic example of eat this 
message!
                Jmol.script(jmolApplet0, data);
        }
        return null;
}

If the above code is in two pages on the same browser (frames, tabs, or 
windows) then they can exchange JmolScript commands independent of the DOM. The 
command to make the change is:

localStorage.setItem('talkToFriend', 'select all:color red');   or   
localStorage.setItem('talkToFriend', 'load $aspirin'); or Etc.

You can try it here:

http://chemagic.com/molecules/VMKMINInotes.htm 
<http://chemagic.com/molecules/VMKMINInotes.htm>

The model kit is in an iFrame. Both pages have the above code. If you go to the 
JS console of the browser - not the Jmol frame, then the containing page will 
be the sender. This page does not even know what Jmol is. It’s a slide show 
page, and it could even be in another tab or window. The DOMs of both pages are 
irrelevant to the dialog. In the console, enter:

localStorage.setItem('talkToFriend', 'select all:color red')

or any other Jmol script. Note that the sending page does not get the event. 
That’s by design. Note also that if the Jmol page were to send a script in this 
case there would be an error. There is no Jmol on the sending page. Tweaking 
the senders event function could prevent an inadvertent event error. For 
example, if the sender never has to listen, the the event function could simply 
return null;

I called this “talkToFriend” because it’s set up for 1 to 1 communication. With 
a little thought, multi frame/window Jmol synchronization should be possible. I 
have to think about this.

Otis

--
Otis Rothenberger
o...@chemagic.com
http://chemagic.com

------------------------------------------------------------------------------
_______________________________________________
Jmol-users mailing list
Jmol-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jmol-users

Reply via email to