lang="en-US"> Useful AJAX Functions –  Design1online.com, LLC

Useful AJAX Functions

/**************
* Purpose: initalize ajax
* Precondition: none
* Postcondition: ajax xmlhttp object running
**************/

//global variables
var xmlhttp = false; //initalize ajax
var queryString;

//check if we're using IE
try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
if (!xmlhttp && typeof XMLHttpRequest != "undefined")
    xmlhttp = new XMLHttpRequest();

    /**************
    * Purpose: make an ajax get request
    **************/
    function getrequest(page, objID)
    {

        //change cursor
        document.body.style.cursor = 'wait';

        //find object
        var obj = document.getElementById(objID);

        //remove tinyMCE editors
        //removeTiny();

        //get information
        xmlhttp.open("GET", page);
        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                //change the page
                obj.innerHTML = xmlhttp.responseText;

                //restore the tinyMCE editors
                //loadTiny();

                //change the cursor back
                document.body.style.cursor = 'default';
            }
        }

        //send information
        xmlhttp.send(null);
    }

    /**************
    * Purpose: make an ajax post request
    **************/
    function postrequest(page, objID)
    {

        //change cursor
        document.body.style.cursor = 'wait';

        //remove tinyMCE editors
        //removeTiny();

        //open the page
        xmlhttp.open("POST", page);

        //send the querystring headers
        //set the querystring variable before you call this...
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        xmlhttp.send(queryString);   

        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
            {

                //change the page
                obj.innerHTML = xmlhttp.responseText;

                //restore the tinyMCE editors
                //loadTiny();

                //put back the cursor
                document.body.style.cursor = 'default';
            }
        }

        if (queryString) //reset query string
            queryString = "";

    }

    /**************
    * Purpose: initialize the top-level variable; also reset the variable to cover when 
    *          the user clicks multiple times   
    **************/
    function setQueryString(num)
    {
        if (num == null)
            num = 0;

        queryString="";
        var frm, numberElements;
        frm = document.forms[num];
        numberElements = frm.elements.length;

        for(var i = 0; i < numberElements; i++) {
            if (i < numberElements-1) {
                queryString += frm.elements[i].name+"="+frm.elements[i].value+"&";
            } else {
                queryString += frm.elements[i].name+"="+frm.elements[i].value;
            }
        }

        //if youre using tinyMCE editors
        //for(id in tinyMCE.editors)
        //        if (id != "rshStorageField" && id != "toJSONString" && id != "mce_0")
        //            queryString += "&" + id + "=" + tinyMCE.get(id).getContent();
    }

    /**************
    * Purpose: load tiny MCE editors 
    * Requires tinyMCE from Moxiecode 
    **************/
    function loadTiny()
    {

        tinyMCE.init({
            // General options
            mode : "textareas",
            theme : "advanced",
            skin : "o2k7",
            skin_variant : "black",
            plugins : "save,safari,pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

            // Theme options
            theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect, |,forecolor,backcolor",
            theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview",
            theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : true

            // Example content CSS (should be your site CSS)
            //content_css : "css/content.css",

            // Drop lists for link/image/media/template dialogs
            //template_external_list_url : "../javascript/tiny_mce/lists/template_list.js",
            //external_link_list_url : "../javascript/tiny_mce/lists/link_list.js",
            //external_image_list_url : "../javascript/tiny_mce/lists/image_list.js",
            //media_external_list_url : "../javascript/tiny_mce/lists/media_list.js"
        });

        return true;
    }

    /**************
    * Purpose: remove tiny MCE editors 
    * Requires tinyMCE from Moxiecode 
    **************/
    function removeTiny()
    {
         if (typeof tinyMCE != "undefined") 
         {
            for(id in tinyMCE.editors)
                if (id != "rshStorageField" && id != "toJSONString")
                tinyMCE.execCommand('mceRemoveControl', false, id);
        }
    }

You may also like...

Leave a Reply