~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: mattgiuca
  • Date: 2008-02-27 00:00:38 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:594
Added to doc/setup/faq.txt: sessions directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
519
519
    arr.splice(j, i-j);
520
520
}
521
521
 
522
 
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
523
 
 * response, and returns an XMLHttpRequest object containing the completed
524
 
 * response.
 
522
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
 
523
 * fashion.
 
524
 */
 
525
function new_xmlhttprequest()
 
526
{
 
527
    try
 
528
    {
 
529
        /* Real Browsers */
 
530
        return new XMLHttpRequest();
 
531
    }
 
532
    catch (e)
 
533
    {
 
534
        /* Internet Explorer */
 
535
        try
 
536
        {
 
537
            return new ActiveXObject("Msxml2.XMLHTTP");
 
538
        }
 
539
        catch (e)
 
540
        {
 
541
            try
 
542
            {
 
543
                return new ActiveXObject("Microsoft.XMLHTTP");
 
544
            }
 
545
            catch (e)
 
546
            {
 
547
                throw("Your browser does not support AJAX. "
 
548
                    + "IVLE requires a modern browser.");
 
549
            }
 
550
        }
 
551
    }
 
552
}
 
553
 
 
554
/** Makes an asynchronous XMLHttpRequest call to the server.
 
555
 * Sends the XMLHttpRequest object containing the completed response to a
 
556
 * specified callback function.
525
557
 *
 
558
 * \param callback A callback function. Will be called when the response is
 
559
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
 
560
 *      completed response.
526
561
 * \param app IVLE app to call (such as "fileservice").
527
562
 * \param path URL path to make the request to, within the application.
528
563
 * \param args Argument object, as described in parse_url and friends.
530
565
 * \param content_type String, optional. Only applies if method is "POST".
531
566
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
532
567
 *      Defaults to "application/x-www-form-urlencoded".
533
 
 * \return An XMLHttpRequest object containing the completed response.
534
568
 */
535
 
function ajax_call(app, path, args, method, content_type)
 
569
function ajax_call(callback, app, path, args, method, content_type)
536
570
{
537
571
    if (content_type != "multipart/form-data")
538
572
        content_type = "application/x-www-form-urlencoded";
542
576
     * (This is not checked against anywhere else, it is solely defined and
543
577
     * used within this function) */
544
578
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
545
 
    var xhr = new XMLHttpRequest();
 
579
    var xhr = new_xmlhttprequest();
 
580
    xhr.onreadystatechange = function()
 
581
        {
 
582
            if (xhr.readyState == 4)
 
583
            {
 
584
                callback(xhr);
 
585
            }
 
586
        }
546
587
    if (method == "GET")
547
588
    {
548
589
        /* GET sends the args in the URL */
549
590
        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);
 
591
        /* open's 3rd argument = true -> asynchronous */
 
592
        xhr.open(method, url, true);
553
593
        xhr.send(null);
554
594
    }
555
595
    else
556
596
    {
557
597
        /* POST sends the args in application/x-www-form-urlencoded */
558
598
        url = encodeURI(path);
559
 
        xhr.open(method, url, false);
 
599
        xhr.open(method, url, true);
560
600
        var message;
561
601
        if (content_type == "multipart/form-data")
562
602
        {
572
612
        xhr.setRequestHeader("Content-Length", message.length);
573
613
        xhr.send(message);
574
614
    }
575
 
    return xhr;
576
615
}
577
616