344
/** Writes a JSONable object to the cookie under a particular key
345
* (JSON encoded and URL encoded).
347
function write_cookie(key, value)
349
var sendstr = encodeURIComponent(key) + "="
350
+ encodeURIComponent(JSON.stringify(value))
351
+ "; path=" + urlencode_path(root_dir);
352
/* This actually just assigns to the key, not replacing the whole cookie
353
* as it appears to. */
354
document.cookie = sendstr;
356
/** Reads a cookie which has a JSONable object encoded as its value.
357
* Returns the object, parsed from JSON.
359
function read_cookie(key)
361
var cookies = document.cookie.split(";");
362
var checkstart = encodeURIComponent(key) + "=";
363
var checklen = checkstart.length;
364
for (var i=0; i<cookies.length; i++)
366
var cookie = cookies[i];
367
while (cookie[0] == ' ')
368
cookie = cookie.substr(1);
369
if (cookie.substr(0, checklen) == checkstart)
371
var valstr = cookie.substr(checklen);
372
valstr = decodeURIComponent(valstr);
373
return JSON.parse(valstr);
344
378
/** Given an argument map, as output in the args parameter of the return of
345
379
* parseurl, gets the first occurence of an argument in the URL string.
346
380
* If the argument was not found, returns null.
519
553
arr.splice(j, i-j);
522
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
523
* response, and returns an XMLHttpRequest object containing the completed
556
/** Shallow-clones an object */
557
function shallow_clone_object(obj)
565
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
568
function new_xmlhttprequest()
573
return new XMLHttpRequest();
577
/* Internet Explorer */
580
return new ActiveXObject("Msxml2.XMLHTTP");
586
return new ActiveXObject("Microsoft.XMLHTTP");
590
throw("Your browser does not support AJAX. "
591
+ "IVLE requires a modern browser.");
597
/** Makes an asynchronous XMLHttpRequest call to the server.
598
* Sends the XMLHttpRequest object containing the completed response to a
599
* specified callback function.
601
* \param callback A callback function. Will be called when the response is
602
* complete. Passed 1 parameter, an XMLHttpRequest object containing the
603
* completed response.
526
604
* \param app IVLE app to call (such as "fileservice").
527
605
* \param path URL path to make the request to, within the application.
528
606
* \param args Argument object, as described in parse_url and friends.
530
608
* \param content_type String, optional. Only applies if method is "POST".
531
609
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
532
610
* Defaults to "application/x-www-form-urlencoded".
533
* \return An XMLHttpRequest object containing the completed response.
535
function ajax_call(app, path, args, method, content_type)
612
function ajax_call(callback, app, path, args, method, content_type)
537
614
if (content_type != "multipart/form-data")
538
615
content_type = "application/x-www-form-urlencoded";
542
619
* (This is not checked against anywhere else, it is solely defined and
543
620
* used within this function) */
544
621
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
545
var xhr = new XMLHttpRequest();
622
var xhr = new_xmlhttprequest();
623
xhr.onreadystatechange = function()
625
if (xhr.readyState == 4)
546
630
if (method == "GET")
548
632
/* GET sends the args in the URL */
549
633
url = build_url({"path": path, "args": args});
550
/* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
551
* (No need for a callback function) */
552
xhr.open(method, url, false);
634
/* open's 3rd argument = true -> asynchronous */
635
xhr.open(method, url, true);
557
640
/* POST sends the args in application/x-www-form-urlencoded */
558
641
url = encodeURI(path);
559
xhr.open(method, url, false);
642
xhr.open(method, url, true);
561
644
if (content_type == "multipart/form-data")