Change Flash Size with Javascript

11 November 2009

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);
    }
}

Vote this post

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...
Categories: Coding
Tags: ,

Comments

Leave a Reply