Resize flash via JS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gconrads
    New Member
    • Aug 2006
    • 2

    Resize flash via JS

    OK, today is my twofer special on js questions.
    BTW: the method used here is working in Firefox

    I'm working on a script that switches the width of a Flash movie between 100% and 812 pixels depending on the innerwidth of the browser window.

    The point is when a flash movie is embedded for fullscreen usage (100%) it acts somewhat like a background, and won't cause scrollbars if the window is scaled to a smaller size than the 'actual' content in the flash. This means that the flash get's clipped and the user can't see some of the site.

    My first solution was to place a 'spacer' div below the flash to 'hold the scrollbars. I set the div to 802 pixels wide (the same size as my movie content) this kept the scroll bars, but interestingly, when the width of the movie is 100% it doesn't expand to the width of the scrollable space, rather is set to the actual width of the window (seems obvious now).

    Because i'm using a background repeat (and i like to figure things out) setting a fixed width for the flash and centering it in a div is not an option. :p Though maybe if I got tricky and centered a background repeat + offset the centering to align with the flash movie....

    OK, so cut to the chase.

    Here is my JS code (working in Firefox, with no javacript errors in the console):


    Code:
    function resetWidth(x) {
        var flash = document.getElementById(x);
        //document.write("hey!");
    
        // get browser width
        var win_width = window.innerWidth;
     
        if (win_width < 812) {
            flash.setAttribute("width", "812");
            //window.width = 812;
        } else {
    	    flash.setAttribute("width", "100%");
        }
    }

    I like short code! that was simpler than my explanation :)


    My HTML:

    [HTML]<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>test_bed _1</title>



    <style type="text/css">
    <!--
    body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    background: #241A0D url(../graphics/pattern.gif);
    }

    p {

    }

    table {
    vertical-align: bottom;
    }

    .footer {
    margin-left: auto;
    margin-right: auto;
    display: block;
    height: 20px;
    width: 802px;
    background: #241A0D;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #493825;
    text-align: right;

    }

    -->
    </style>

    <script type="text/javascript" src="../scripts/width.js"></script>

    </head>
    <body onresize="reset Width('flashme' );">

    <!--url's used in the movie-->
    <!--text used in the movie-->
    <!-- saved from url=(0013)about :internet -->
    <OBJECT classid="clsid: D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macrom edia.com/pub/shockwave/cabs/flash/SWFlash.cab#ver sion=7,0,0,0"
    id="stage" ALIGN="top">
    <PARAM NAME=menu VALUE=false>
    <PARAM NAME=quality VALUE=high>
    <PARAM NAME=salign VALUE=LT>
    <PARAM NAME=bgcolor VALUE=#999999>
    <PARAM NAME=FlashVars value="allowRes ize=true">
    <EMBED src="RG_build_1 .swf" id="flashme" menu="false" quality="high" salign="LT" bgcolor="#99999 9" width="100%" HEIGHT="780px" NAME="stage" ALIGN="top" TYPE="applicati on/x-shockwave-flash" PLUGINSPAGE="ht tp://www.macromedia. com/go/getflashplayer" >
    </EMBED>
    </OBJECT>
    <div class="footer"> RG</div>
    </body>
    </html>[/HTML]

    *Notice that for now I'm calling the 'resetWidth' function 'onresize' and I'm targeting the embeded tag via the ID 'flashme' :D

    *I put my styles in the head so you can see those too.

    *Totally excited to see if someone has a great solution for this or recommendation of a better / alternate methods. My goal is simplicity.
  • malcolmnew
    New Member
    • Jul 2007
    • 1

    #2
    Very helpful and it works in Netscape and Internet Explored with the important adaptations. You should also resize the height, the onResize should be supplemented by onLoad so you can target differing resolutions and window.innerwid th does not work with IE so you have to supply alternate code.
    Here is my operating function:
    [CODE=javascript]function resetwidth(){
    var flash = document.getEle mentById("movie ");
    browserName = navigator.appNa me;
    if (browserName == 'Netscape'){
    var winwidth = window.innerWid th;}
    else{
    var winwidth = document.body.c lientWidth;}
    if (winwidth > 800){
    flash.setAttrib ute("width","80 0");
    flash.setAttrib ute("height","1 92");
    }else{
    flash.setAttrib ute("width","61 0");
    flash.setAttrib ute("height","1 47");
    }
    }[/CODE]
    Hope this is of interest
    Last edited by acoder; Jul 11 '07, 09:36 AM. Reason: Code in tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by malcolmnew
      Very helpful and it works in Netscape and Internet Explored with the important adaptations. You should also resize the height, the onResize should be supplemented by onLoad so you can target differing resolutions and window.innerwid th does not work with IE so you have to supply alternate code.
      Here is my operating function:
      [CODE=javascript]function resetwidth(){
      var flash = document.getEle mentById("movie ");
      browserName = navigator.appNa me;
      if (browserName == 'Netscape'){
      var winwidth = window.innerWid th;}
      else{
      var winwidth = document.body.c lientWidth;}
      if (winwidth > 800){
      flash.setAttrib ute("width","80 0");
      flash.setAttrib ute("height","1 92");
      }else{
      flash.setAttrib ute("width","61 0");
      flash.setAttrib ute("height","1 47");
      }
      }[/CODE]
      Hope this is of interest
      Instead of using browser detection, try something that would work in any browser based on support of a property. See this page which shows the properties supported by a particular browser and their values.

      Comment

      • angelicdevil
        New Member
        • Apr 2009
        • 116

        #4
        this i did but its not working

        the html file is as follows :-

        Code:
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>test_bed_1</title>
        
        
        
        <style type="text/css">
        <!--
        body {
        margin-left: 0px;
        margin-top: 0px;
        margin-right: 0px;
        margin-bottom: 0px;
        }
        
        p {
        
        }
        
        table {
        vertical-align: bottom;
        }
        
        .footer {
        margin-left: auto;
        margin-right: auto;
        display: block;
        height: 20px;
        width: 802px;
        background: #241A0D;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 10px;
        color: #493825;
        text-align: right;
        
        }
        
        -->
        </style>
        
        <script type="text/javascript" src="d:/width.js"></script>
        
        </head>
        <body onresize="resetWidth('flashme');">
        
        <!--url's used in the movie-->
        <!--text used in the movie-->
        <!-- saved from url=(0013)about:internet -->
        <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/SWFlash.cab#version=7,0,0,0"
        id="stage" ALIGN="top">
        <PARAM NAME=menu VALUE=false>
        <PARAM NAME=quality VALUE=high>
        <PARAM NAME=salign VALUE=LT>
        <PARAM NAME=bgcolor VALUE=#999999>
        <PARAM NAME=FlashVars value="allowResize=true">
        <EMBED src="lucky.swf" id="flashme" menu="false" quality="high" salign="LT" bgcolor="#999999" width="100%" HEIGHT="100%" NAME="stage" ALIGN="top" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
        </EMBED>
        </OBJECT>
        <div class="footer">RG</div>
        </body>
        </html>

        and the function javascript is as

        Code:
        function resetWidth(x) {
            var flash = document.getElementById(x);
            //document.write("hey!");
         
            // get browser width
            var win_width = window.innerWidth;
         
            if (win_width <= 800) {
                flash.setAttribute("width", "800");
                flash.setAttribute("height", "600");
                //window.width = 812;
            } else {
                flash.setAttribute("width", "1024");
                flash.setAttribute("height", "768");
            }
        }
        still its not resizing plz help
        Last edited by acoder; Apr 22 '09, 09:34 AM. Reason: Added [code] tags

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Set the units as well, i.e.
          Code:
          flash.setAttribute("width", "800px");

          Comment

          • gopan
            New Member
            • Apr 2009
            • 41

            #6
            Check this in your code...

            Code:
              // get browser width
              var win_width = window.innerWidth;
            the window.innerWid th is Netscape/Mozilla compatible (Read/Write) and not supported by IE

            so you need to use

            Code:
            // check for the browser type
            if(ie)
             var win_width = body.clientWidth;
            else
              var win_width = window.innerWidth;
            the body.clientWidt h is IE compatible (Read only) and not supported by Netscape/Mozilla < v7

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              That would be document.body.c lientWidth. Instead of browser detection, just check that innerWidth is supported:
              Code:
              if (window.innerWidth) {
              ...
              } else if (document.body.clientWidth) {
               ...
              }

              Comment

              • angelicdevil
                New Member
                • Apr 2009
                • 116

                #8
                still not working

                i did wat u suggested but tis not working infact now i m getting some nos displayed above the swf file...for clarity i m posting it as attachment please check...and changes u want to make to get it working feel free and then upload it so i can check and compare and see where i went wrong..

                help appreciated very much thanx

                angel
                Attached Files

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Those are the line numbers from the code. Before you copy the code, remove the line numbers by clicking the Line Numbers link above the code block.

                  PS. if the code is that short, there's no need to attach it - just post the code. It's much quicker that way, thanks.

                  Comment

                  • angelicdevil
                    New Member
                    • Apr 2009
                    • 116

                    #10
                    its still not resizing but yea as u said i did and the nos.a have gone now



                    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                    <title>resize </title>



                    <style type="text/css">
                    <!--
                    10. body {
                    11. margin-left: 0px;
                    12. margin-top: 0px;
                    13. margin-right: 0px;
                    14. margin-bottom: 0px;
                    15. }
                    16.
                    17. p {
                    18.
                    19. }
                    20.
                    21. table {
                    22. vertical-align: bottom;
                    23. }
                    24.
                    25. .footer {
                    26. margin-left: auto;
                    27. margin-right: auto;
                    28. display: block;
                    29. height: 20px;
                    30. width: 802px;
                    31. background: #241A0D;
                    32. font-family: Verdana, Arial, Helvetica, sans-serif;
                    33. font-size: 10px;
                    34. color: #493825;
                    35. text-align: right;
                    36.
                    37. }
                    38.
                    39. -->
                    </style>

                    <script type="text/javascript" src="width.js"> </script>

                    </head>
                    <body onresize="reset Width('flashme' )";>
                    <script language="javas cript">
                    function resetWidth(x) {
                    var flash = document.getEle mentById(x);
                    if (window.innerWi dth <=1024 and window.innerWid th => 800 ) {


                    flash.setAttrib ute("width", "1000px");
                    flash.setAttrib ute("height", "768px");

                    } else if (document.body. clientWidth) {
                    flash.setAttrib ute("width", "800px");
                    flash.setAttrib ute("height", "600px");

                    }
                    }
                    </script>
                    <!--url's used in the movie-->
                    <!--text used in the movie-->
                    <!-- saved from url=(0013)about :internet -->
                    <OBJECT classid="clsid: D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macrom edia.com/pub/shockwave/cabs/flash/SWFlash.cab#ver sion=7,0,0,0"
                    id="stage" ALIGN="top">
                    <PARAM NAME=menu VALUE=false>
                    <PARAM NAME=quality VALUE=high>
                    <PARAM NAME=salign VALUE=LT>
                    <PARAM NAME=bgcolor VALUE=#999999>
                    <PARAM NAME=FlashVars value="allowRes ize=true">
                    <EMBED src="resize.swf " id="flashme" menu="false" quality="high" salign="LT" bgcolor="#99999 9" width="100%" HEIGHT="100%" NAME="stage" ALIGN="top" TYPE="applicati on/x-shockwave-flash" PLUGINSPAGE="ht tp://www.macromedia. com/go/getflashplayer" >
                    </EMBED>
                    </OBJECT>
                    <div class="footer"> </div>
                    </body>
                    </html>

                    Comment

                    • gopan
                      New Member
                      • Apr 2009
                      • 41

                      #11
                      Check this ... I've made some modifications to your script. But when you copy this code first click on the "Line Numbers" to turn it off and click "Select" and copy the code...

                      Code:
                      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
                      <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                      <title>resize</title>
                      
                      <style type="text/css">
                      <!--
                       body { margin: 0 0 0 0; }
                       p { }
                       table {
                       vertical-align: bottom;
                       }
                       .footer {
                       margin-left: auto;
                       margin-right: auto;
                       display: block;
                       height: 20px;
                       width: 802px;
                       background: #241A0D;
                       font-family: Verdana, Arial, Helvetica, sans-serif;
                       font-size: 10px;
                       color: #493825;
                       text-align: right;
                       }
                       -->
                      </style>
                      
                      <script type="text/javascript" src="width.js">
                      	// I dunno why you used this external script???
                      </script>
                      
                      <script language="javascript">
                      function resetWidth(x) {
                      	var flash = document.getElementById(x);
                      	var winWidth = document.body.clientWidth?document.body.clientWidth:window.innerWidth;
                      
                      	if (winWidth <=1024 and winWidth => 800 ) {
                      		flash.setAttribute("width", "1000px");
                      		flash.setAttribute("height", "768px");
                      	} else{
                      		flash.setAttribute("width", "800px");
                      		flash.setAttribute("height", "600px");
                      	}
                      }
                      </script>
                      
                      </head>
                      <body onresize="resetWidth('flashme')";>
                      <!--url's used in the movie-->
                      <!--text used in the movie-->
                      <!-- saved from url=(0013)about:internet -->
                      <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/SWFlash.cab#version=7,0,0,0"
                      id="stage" ALIGN="top">
                      <PARAM NAME=menu VALUE=false>
                      <PARAM NAME=quality VALUE=high>
                      <PARAM NAME=salign VALUE=LT>
                      <PARAM NAME=bgcolor VALUE=#999999>
                      <PARAM NAME=FlashVars value="allowResize=true">
                      <EMBED src="resize.swf" id="flashme" menu="false" quality="high" salign="LT" bgcolor="#999999" width="100%" HEIGHT="100%" NAME="stage" ALIGN="top" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
                      </EMBED>
                      </OBJECT>
                      <div class="footer"></div>
                      </body>
                      </html>
                      Hope this help ya!!

                      Comment

                      • angelicdevil
                        New Member
                        • Apr 2009
                        • 116

                        #12
                        still not working

                        nope still not working

                        Comment

                        • angelicdevil
                          New Member
                          • Apr 2009
                          • 116

                          #13
                          its not adding a black rectangle below the swf file

                          Comment

                          • angelicdevil
                            New Member
                            • Apr 2009
                            • 116

                            #14
                            its not resizingthe swf to fit the screen

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              A few changes:
                              1. Remove lines 28-30.
                              2. Change the "and" on line 37 to "&&".
                              3. Remove the semi-colon on line 48 (the body tag).

                              Comment

                              Working...