### Web Services - Sample 05: Using XMLHttpRequest from JavaScript

Now we complicate the situation by relying upon the XMLHttpRequest method which sits at the heart of AJAX. TODO: change tone to explain why this is beneficial However, in this example, we stick with simple synchronous requests which can return results and state information from PyMOL.

quit pymol (FireFox only: view page source)

Structures

    loadPDB('1iqj'); refreshTable();

    loadPDB('2fzz'); refreshTable();

    loadPDB('2g00'); refreshTable();

    loadPDB('missing'); refreshTable(); (will display Python exception / traceback)

    cmd('delete?name=all'); refreshTable();

Likewise, the table below is updated from JavaScript, based on what objects are currently loaded inside PyMOL.

TODO make these buttons behave better

    refreshTable()

The alignAll command source below illustrates how you can program PyMOL from JavaScript using synchronous requests which return JSON object results from PyMOL that are then subsequently acted upon.

    function alignAll() { // Javascript loop which performs the alignment

        var obj_list = cmd("get_names"); // get current objects

        for(i=1;i<obj_list.length;i++) { // align all to first object

            result = cmd("align?mobile=" + obj_list[i] + "////CA" +
                         "&target=" + obj_list[0] + "////CA");
        }

        if(obj_list.length) {
            cmd("center?selection=" + obj_list[0]); // recenter on first object
        }
    }

Actions

    cmd('zoom?animate=2')

    cmd('show_as?representation=ribbon')

    alignAll()