Posts Tagged ‘javascript’

Mini Ajax Library

26 December 2009

You need basic ajax functions and you need a simple, small library not like those big, complex libraries like prototype. Well, open source community hears you. MiniAjax is a lightweight ajax library which is only 1.4 kb without compression. MiniAjax can do simple form serializing, send request to a url, get its response by both GET or POST methods and insert the result in a html element if needed, submit a form.

Here is the Google Code page of this library: http://code.google.com/p/miniajax/ (Everything in one page). You can find discussions and some extra functions at DZone Snippets

Here is the crossbrowser javascript code for changing flash object’s size. Works fine in IE5.5, IE6, IE7, IE8, Firefox 3, Opera 10, Safari 4, Chrome 3.

Usage
Resizing Flash:
flashResizer.resize(width, height, flash_object_id);

Zoom In:
flashResizer.zoomin(flash_object_id);

Zoom Out:
flashResizer.zoomout(flash_object_id);

Javascript Code:

var flashResizer = {
    ZOOM_IN:1,ZOOM_OUT:-1,
    fo:null,ZOOM_FACTOR:0.1,
    findObj:function(_foid){if(_foid==undefined || _foid==null){return this.fo;}
        this.fo=null;if (window.document[_foid]) this.fo=window.document[_foid];
        else if (navigator.appName.indexOf("Microsoft Internet")==-1){
            if (document.embeds && document.embeds[_foid]) this.fo = document.embeds[_foid];}
        else this.fo = document.getElementById(_foid);return this.fo;},
    resize:function(_w,_h,_foid){if(this.findObj(_foid)){
            this.fo.width=_w;this.fo.height=_h;}return this;},
    zoom:function(_d,_f){if(this.findObj(_f)){var zf=1+_d*this.ZOOM_FACTOR;
            this.resize(this.fo.width*zf,this.fo.height*zf);}return this;},
    zoomin:function(_f){this.zoom(this.ZOOM_IN,_f);},
    zoomout:function(_f){this.zoom(this.ZOOM_OUT, _f);}
}

Same Javascript Code With Comments (Longer Code):

var flashResizer = {
    fo:null, //flash object
    ZOOM_FACTOR:0.1, // how much resizing will happen. used in zoom

    ZOOM_IN:1,
    ZOOM_OUT:-1,

    /**
     * find flash object - cross browser
     * look at http://blog.codefidelity.com/?p=14
     * @param _foid  DOM id of flash object. if no _foid is given,
     *               uses previously found flash object
     * @return       flash object. null if no object is found
     */
    findObj:function(_foid){
        if(_foid==undefined || _foid==null){
           return this.fo;
        }
        this.fo=null;
        if (window.document[_foid]) this.fo=window.document[_foid];
        else if (navigator.appName.indexOf("Microsoft Internet")==-1){
            if (document.embeds && document.embeds[_foid]) this.fo = document.embeds[_foid];
        }
        else this.fo = document.getElementById(_foid);

        return this.fo;
    },

    /**
     * resize flash to specified size
     * @param _w: new width
     * @param _h: new height
     * @param _foid: flash object id. if no _foid is given,
     *               uses previously found flash object.
     * @return this object
     */
    resize:function(_w, _h, _foid){
        if(this.findObj(_foid)){
            this.fo.width = _w;
            this.fo.height = _h;
        }
        return this;
    },

    /**
    * @param _d: zoom direction. flashResize.ZOOM_IN or 1 to zoom in,
    *            flashResize.ZOOM_OUT or-1 to zoom out.
    *            factor size also changes the zoom factor
    * @param _foid: flash object id. if no _foid is given,
    *               uses previously found flash object.
    * @return  this object
    */
    zoom:function(_d, _foid){
        if(this.findObj(_foid)){
            var zf = 1 + _d * this.ZOOM_FACTOR;
            this.resize(this.fo.width*zf, this.fo.height*zf);
        }
        return this;
    },

    /**
     * @param _foid: flash id. if no _foid is given, uses previously found flash object.
     */
    zoomin:function(_foid){
        return this.zoom(this.ZOOM_IN, _foid);
    },

    /**
     * @param _foid: flash id. if no _foid is given, uses previously found flash object.
     */
    zoomout:function(_foid){
        return this.zoom(this.ZOOM_OUT, _foid);
    }
}
Categories: Coding
Tags: ,
continue >