SetM on Marvin View generates javascript error

User d727ff5bc5

22-09-2010 13:04:20

Hi,


I have a parent html page containing an iFrame which will be updated on-the-fly; the iFrame contains a MarvinView table.
The code of the iFrame is the following:


<html>
    <head>
        <script LANGUAGE="JavaScript1.1" SRC="../ChemAxon/JChem/marvin/marvin.js"></script>
        <script language="JavaScript" type="text/javascript">
        function Populate_MView() {
            x = parent.XMLMols.getElementsByTagName("ENTRIES")[0];
            for (var i = 0; i < x.childNodes.length; i++) {
                XMLMol = x.childNodes;
                for (var j = 0; j < XMLMol.childNodes.length; j++) {
                    if (XMLMol.childNodes[j].nodeName == "SMILES") {
                        var smi = XMLMol.childNodes[j].text;
                        do {
                            var err = null;
                            try {
                                document.MView.SetM(i*5, smi);
                            }
                            catch (err) {
                                alert(err.description);
                            }
                        } while (err != null);
                    }
                }
            }
            document.MView.setSelectedIndex((x.childNodes.length - 1)*5);
        }
        </script>
    </head>
    <body>
        <script language="JavaScript" type="text/javascript">
            mview_name = "MView";
            mview_begin("../ChemAxon/JChem/marvin", 900, 200);
            mview_param("cols", "5");
            mview_param("rows", "1");
            mview_param("visibleRows", "2");
            mview_param("visibleCols", "5");
            mview_end();
            Populate_MView();
        </script>
    </body>
</html>

The parent page contains a XML object with several entries, one smiles per entry.


On Windows XP + Internet Explorer 6 + Java 1.6 + Marvin 5.3.7, everything works fine.
On Windows 7 + Internet Explorer 8 + Java 1.6 + Marvin 5.3.7, the MarvinView Table in the iFrame will be created but the Javascript will generate an error at document.MView.SetM, and the corresponding error description is "Object doesn't support this property or method".


Interestingly, after the alert pops up, the document.MView.SetM commands works perfectly for all subsequent tries, as well as document.MView.selectedIndex. It seems that my Populate_MView function is fired too early, before the MarvinView Table is created/available.


Any hint of how to solve my issue? Is there any function available that I can call within a loop in Javascript to wait for the MarvinView object to be available? I tried to fire my Populate_MView function in the body onLoad event, but the code hangs at the same place.


Thanks for your help!


Best,



Grégori

ChemAxon 5433b8e56b

23-09-2010 20:12:16

Hi Grégori,


it is always a mistery when the javascript calls will run. It depends on the browsers.


In your case, the browser will run the javascript block as it is, and the first part will generate the proper html code to embed the applet into the html page. Here is some kind of synchronization or control flow issue happens, and any try to reach the applet thru document.MView will not reach the applet at that time, it seems the DOM changes will not populate immediatelly after document.write().


I don't really know why it is happening, but if you call the Populate_MView method like this setTimeout("Populate_MView()", 0) it will work, and the function call will see the changes in DOM.


Regards,
Istvan

User d727ff5bc5

24-09-2010 09:01:33

Hi Istvan,


Yes it did the trick! Thanks a lot for your answer!


Grégori