1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
<html>
<head>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Expires" content="-1" />
<title>Web Services - Sample 13: Using jQuery with the PyMOL web server</title>
<script src="json2.js"></script>
<script src="pymol.js"></script>
<script src="jquery.js"></script>
<script type="text/javascript">
// This page assumes that PyMOL is running on 8083
var pymolhost = "http://localhost:8083";
function callback1(result)
{
document.getElementById('result').innerHTML = "<pre>"+result+"</pre>";
}
$(document).ready(function() {
$("a[name=load]").click(function() {
try {
$.getJSON(pymolhost+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?", callback1);
} catch (e) {
alert(e);
}
return false; // causes the HREF to not load a new page
});
$("a[name=delete]").click(function() {
try {
$.getJSON(pymolhost+"/apply/pymol.cmd.delete?name=pept&_callback=?", callback1);
} catch (e) {
alert(e);
}
return false; // causes the HREF to not load a new page
});
$("a[name=get_names]").click(function() {
try {
$.getJSON(pymolhost+"/apply/pymol.cmd.get_names?type=objects&_callback=?", callback1);
} catch (e) {
alert(e);
}
return false; // causes the HREF to not load a new page
});
$("a[name=badload]").click(function() {
try {
$.getJSON(pymolhost+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb");
} catch (e) {
if (e.description == undefined) {
alert(e); //Firefox
} else {
alert(e.description); //Explorer
}
}
return false; // causes the HREF to not load a new page
});
});
</script>
<style text/css>
a.info {
text-decoration:underline;
background-color: transparent;
}
a{
text-decoration:none;
padding: 1px;
background-color: #dddddd
}
</style>
</head>
<body>
<h3>Web Services - Sample 13: Using jQuery with the PyMOL web server</h3>
<a href="javascript:void(0)" onclick="window.open('view-source:' + location.href)">view page source</a>
<a href="http://localhost:8083/apply/_quit?href">quit pymol</a>
<p>
<div id="debug">
</div>
PyMOL results will appear here
<div id="result" style="border-style: solid; padding: 10">
</div>
</p>
<p>
A simple
<A class=info HREF="http://jquery.com/" TARGET=_blank>jQuery</A>
and JSON function is $.getJSON. This function accepts a URL and the
name of a JavaScript function to call after the URL result arrives.
<pre>
<A HREF="javascript:$.getJSON('http://localhost:8083/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?', callback1)">load</A>
</pre>
causes this
<A HREF="javascript:$.getJSON('http://localhost:8083/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?', callback1)">load</A>
</A>
link to appear.
The callback1 function simply places the result into the black-border box
above. You can view this callback1 function by
using the "view page source" link above.
</p>
<p>Next, let's
<A HREF="javascript:$.getJSON('http://localhost:8083/apply/pymol.cmd.delete?name=pept&_callback=?', callback1)">delete</A>
</A>
this object and show another common way of using jQuery.
<p>
This html snippet
<pre>
<a href="javascript:void(0)" name=load>load</a>
</pre>
defines a
<a href="javascript:void(0)" name=load>load</a>
link that is much simpler,
putting the responsibilty on the jQuery $("a[name=load]").click property
to define how to handle this link.
This definition occurs in the jQuery $(document).ready function
<pre>
$(document).ready(function() {
$("a[name=load]").click(function() {
try {
$.getJSON(host+"/apply/pymol.cmd.load?filename=$PYMOL_PATH/test/dat/pept.pdb&_callback=?", callback1);
} catch (e) {
alert(e);
}
return false; // causes the HREF to not load a new page
});
});
</pre>
The full source can
be seen by following the "view page source" link above.
</p>
<p>
Other links can be defined in a similar way, for example to
<a href="javascript:void(0)" name=get_names>get object names</a>
or to
<a href="javascript:void(0)" name=delete>delete</a> the object we loaded.
</p>
<p>
Sample16 shows how you can construct complex URL's containing multiple
pymol commands. These URL strings can be used in the $.getJSON function
shown above.
</p>
<p><b>jQuery and cross-domain scripting</b></p>
<p>
This document came from
<script>document.write(document.URL)</script>
as shown in the URL location bar of this browser.
The jQuery requests are made to the PyMOL server at
<script>document.write(pymolhost)</script>
This is a cross-domain request that violates the same-origin policy.
But jQuery uses a workaround called cross-domain scripting with JSONP
that allows a callback to process any response from the PyMOL server.
In order to illustrate the necessity of a callback in this situation,
click on this link to get an error alert.
<br>
<a href="javascript:void(0)" name=badload>badload</a>
</p>
<!-- the only purpose of this IFRAME is to launch PyMOL via the PWG
-- helper mechanism -->
<iframe src="start8083.pwg" name=pwglaunch
width="0" height="0" frameborder="0"
marginheight="0" marginwidth="0"
scrolling="auto"></iframe>
</body>
</html>
|