Sep 7, 2011

Change SWF height/width dynamically using Javascript/jQuery

Many time you need to have swf re-sized based upon your need. E.g. you don't want to show vertical scroll bar when content grows in flash, or you don't want that extra content to be cut because of fix flash height, or anything else.
Changing swf size is very simple using jQuery. Just call this JS function to make it happen:


function UpdateSWFSize(w, h) {
    if ($("#my-swf-id")) {
        // For IE
        $("#my-swf-id").height(h);
        $("#my-swf-id").width(w);

        // For FF, Chrome, Opera, Safari
        var swf_embed = document.getElementsByTagName("embed")[0];
        $(swf_embed).height(h);
        $(swf_embed).width(w);
    }
}

You can also do it without jQuery in this way:


function UpdateSWFSize(w, h) {
    if (document.getElementById("my-swf-id") ) {
        // For IE
        document.getElementById("my-swf-id").height = h;

        document.getElementById("my-swf-id").width = w;


        // For FF, Chrome, Opera, Safari
        var swf_embed = document.getElementsByTagName("embed")[0];
        swf_embed.height = h;
        swf_embed.width = w;
    }
}




Off course you must have included jQuery Javascript file in your page and "my-swf-id" is to be replaced with your actual swf id.

1 comment:

  1. abe kuchch aata hai tujhe ke tip kar mere blog se copy mar kar likh deta hai .........

    ReplyDelete

I love comments.