new and restructured javascript examples and documentation (JAL-816,
[jalview.git] / examples / javascriptLaunch.html
1 <html>
2   <head><title>Opening JalviewLite from Javascript</title>
3 </head>
4   <body>
5   <SCRIPT type="text/javascript">
6   /* <![CDATA[ // */
7 // From http://snipplr.com/view.php?codeview&id=1272
8 //----------------------------------------
9 //Wrapper function for constructing a request object.
10 //      Parameters:
11 //              reqType: The HTTP request type, such as GET or POST.
12 //              url: The URL of the server program.
13 //              asynch: Whether to send the request asynchronously or not.
14 //----------------------------------------
15
16 function httpRequest(reqType,url,asynch,respHandle) {
17
18         // Mozilla-based browsers
19         if (window.XMLHttpRequest) {
20                 request = new XMLHttpRequest();
21         } else if (window.ActiveXObject) {
22                 request = new ActiveXObject("Msxml2.XMLHTTP");
23                 if (!request) {
24                         request = new ActiveXObject("Microsoft.XMLHTTP");
25                 }
26         }
27         
28         // Request could still be null if neither ActiveXObject
29         //   initialization succeeded
30         if (request) {
31                 // If the reqType param is POST, then the fifth arg is the POSTed data
32                 if (reqType.toLowerCase() != "post") {
33                         initReq(reqType, url, asynch, respHandle);
34                 } else {
35                         // The POSTed data
36                         var args = arguments[5];
37                         if (args != null && args.length > 0) {
38                                 initReq(reqType, url, asynch, respHandle, args);
39                         }
40                 }
41         } else {
42                 alert("Your browser does not permit the use of all " +
43                         "of this application's features!");
44         }
45
46 }
47
48 //----------------------------------------
49 //Initialize a request object that is already constructed
50 //----------------------------------------
51
52 function initReq(reqType, url, bool, respHandle) {
53         try {
54                 // Specify the function that will handle the HTTP response
55                 request.onreadystatechange = respHandle;
56                 request.open(reqType, url, bool);
57                 // If the reqType param is POST, then the
58                 //   fifth argument to the function is the POSTed data
59                 if (reqType.toLowerCase() == "post") {
60                         // Set the Content-Type header for a POST request
61                         request.setRequestHeader("Content-Type", "application/x-ww-form-urlencoded; charset=UTF-8");
62                         request.send(arguments[4]);
63                 } else {
64                         request.send(null);
65                 }
66         } catch (errv) {
67                 alert("The application cannot contact the server at the moment. " +
68                         "Please try again in a few seconds.\n" +
69                         "Error detail: " + errv.message);
70         }
71 }
72
73 // jalview launching with fetched data
74
75 function startJalview(aligURL,title,alwvar) {
76                 var aligment = "";
77                 httpRequest("get",aligURL,true,function() {
78                                 if (request.readyState == 4) { 
79                                         alignment = request.responseText; 
80                                         eval("var "+alwvar+" = document.JalviewLite.loadAlignment(alignment,title)");
81                                 }
82                 })
83                 
84 }
85
86 /* ]]> */
87 </SCRIPT>
88   <form name="Form1">
89 <applet name="JalviewLite"  code="jalview.bin.JalviewLite"
90 archive="jalviewApplet.jar" width="0" height="0">
91 <param name="debug" value="true"/>
92 <param name="showbutton" value="false"/>
93 </applet>
94
95
96   <input type="button" name="Button1" value="Start"
97 onClick="startJalview('plantfdx.fa','Button1.alignment','alwvar')"/>
98   </form>
99   
100
101   </body>
102 </html>