www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 05-04-2005, 05:01 PM
    mshaheen mshaheen is offline
    Registered User
     
    Join Date: May 2005
    Posts: 4
    Maintain Window aspect ratio onResize

    Does anyone have a script, or know of a way to maintain the aspect ratio of the window when a user resizes it?
    Reply With Quote
      #2  
    Old 05-04-2005, 05:21 PM
    nzakas nzakas is offline
    JavaScript Geek
     
    Join Date: May 2005
    Posts: 84
    Sure, first figure out the ratio (this should be done onload):

    Code:
    //look for browser-specific measurements
    var iWidth = (typeof self.innerWidth == "number" ? self.innerWidth : document.body.clientWidth);
    var iHeight = (typeof self.innerHeight == "number" ? self.innerHeight : document.body.clientHeight);
    
    //get ratio
    var fRatio = iWidth/iHeight;
    Then onresize, determine if you want the width or the height to control the new dimensions. If it's width, do this:

    Code:
    var iNewWidth = (typeof self.innerWidth == "number" ? self.innerWidth : document.body.clientWidth);
    
    var iNewHeight = Math.round(iNewWidth * (1/fRatio));
    window.resizeTo(iNewWidth, iNewHeight);
    __________________
    Nicholas C. Zakas
    Author, Professional Ajax
    Author, Professional JavaScript for Web Developers
    Homepage: http://www.nczonline.net/
    Reply With Quote
      #3  
    Old 05-05-2005, 09:34 AM
    mshaheen mshaheen is offline
    Registered User
     
    Join Date: May 2005
    Posts: 4
    Too good to be true

    I knew that seemed too simple. Won't this create an infinite loop? In fact, just using this script gives me an access denied error. In order to see any effect, I placed an alert right before the resize. But, back to my point. Doesn't the resizeTo method trigger the onResize event? So, it will continuously try to adjust the size.

    Code:
    <script language="JavaScript" type="text/JavaScript">
    var fRatio;
    function getRatio() {
    	//look for browser-specific measurements
    	var iWidth = (typeof self.innerWidth == "number" ? self.innerWidth : document.body.clientWidth);
    	var iHeight = (typeof self.innerHeight == "number" ? self.innerHeight : document.body.clientHeight);
    
    	//get ratio
    	fRatio = iWidth/iHeight;
    }
    
    function adjustSize() {
    	var iNewWidth = (typeof self.innerWidth == "number" ? self.innerWidth : document.body.clientWidth);
    
    	var iNewHeight = Math.round(iNewWidth * (1/fRatio));
    
    	alert(fRatio);
    	window.resizeTo(iNewWidth, iNewHeight);
    }
    
    </script>
    </HEAD>
    
    <BODY scroll="no" onLoad="getRatio();" onResize="adjustSize();">
    Reply With Quote
      #4  
    Old 05-05-2005, 11:16 AM
    nzakas nzakas is offline
    JavaScript Geek
     
    Join Date: May 2005
    Posts: 84
    Good point, that's what I get for quickly typing a response while heading home from work!

    This should work in Firefox/Mozilla:

    Code:
    var fRatio;
    var bDontFire = false;
    
    function getRatio() {
    	fRatio = top.outerWidth/top.outerHeight;
    }
    
    function adjustSize() {
        if (bDontFire) return;
    
            if (top.outerWidth/top.outerHeight != fRatio) {
                var iNewHeight = Math.round(top.outerWidth * (1/fRatio));
                bDontFire = true;
    	          window.resizeTo(top.outerWidth, iNewHeight);
                bDontFire = false;       
            }             
    }
    The problem in IE is that you can't get the actual width and height of the window itself, only of the content area (the document body), making this a tough problem to solve. You could start out by resizing the window to a particular size, then getting the width and height of the body to make the calculations:

    Code:
    self.resizeTo(500,400);
    var iDiffWidth = 500 - document.body.clientWidth;
    var iDiffHeight = 400 - document.body.clientHeight;
    Then later you could calculate the ratio as:

    Code:
    fRatio = (document.body.clientWidth + iDiffWidth)/(document.body.clientHeight + iDiffHeight);
    And then you could calculate the new window width, height, and ratio like this:

    Code:
    var iNewWidth = document.body.clientWidth + iDiffWidth;
    var iNewHeight = document.body.clientHeight + iDiffHeight;
    var fNewRatio = iNewWidth/iNewHeight;
    And then go from there (sorry, I don't have time to test all that out right now).
    __________________
    Nicholas C. Zakas
    Author, Professional Ajax
    Author, Professional JavaScript for Web Developers
    Homepage: http://www.nczonline.net/
    Reply With Quote
      #5  
    Old 05-05-2005, 02:24 PM
    A1ien51's Avatar
    A1ien51 A1ien51 is offline
    Ajax Author
     
    Join Date: May 2003
    Location: Between Baltimore and DC
    Posts: 3,362
    access denied error....That error normally occurs with cross domain scripting....
    __________________
    Tech Author [Ajax In Action, javascript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com
    Reply With Quote
      #6  
    Old 05-05-2005, 02:43 PM
    mshaheen mshaheen is offline
    Registered User
     
    Join Date: May 2005
    Posts: 4
    Not sure I know exactly what you mean. But, I can tell you that I am working solely on my local machine right now. Using a text editor and testing using localhost. So, I wouldn't think it should have anything to do with domains. But, I'm no expert. I think I have to throw in the towell on this because of time constraints. I'm not giving up completely, but now the quest is solely because I can't stand to not get something to work properly.

    So, please, everyone, don't kill yourselves over this issue, but if anyone does find ways to get this feature to work, you will be my hero. And, once it is working, i definitely will use it in our final app.
    Reply With Quote
      #7  
    Old 05-05-2005, 02:47 PM
    A1ien51's Avatar
    A1ien51 A1ien51 is offline
    Ajax Author
     
    Join Date: May 2003
    Location: Between Baltimore and DC
    Posts: 3,362
    I have a piece of code here that forces a min. size of a page, you might want to look at how I do the sizing there

    http://radio.javaranch.com/pascarell...571848000.html

    Eric
    __________________
    Tech Author [Ajax In Action, javascript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com
    Reply With Quote
      #8  
    Old 05-05-2005, 03:00 PM
    mshaheen mshaheen is offline
    Registered User
     
    Join Date: May 2005
    Posts: 4
    that does give me some ideas for another way of getting my desired result. HOWEVER, I tried your script verbatim and I get the access denied error when it tries to do the resizeTo. Now, this script has obviously worked for you in the past. What could be causing the access error?
    Reply With Quote
      #9  
    Old 07-30-2008, 01:05 PM
    0xG 0xG is offline
    Registered User
     
    Join Date: Jul 2008
    Posts: 1
    Timing Is Everything

    I struggled with this for some time, too.
    Here is the solution:

    This is due to a race condition; the window has not actually finished loading, and so is not ready to accept a resize command. Sure, it "works" for some people, probably because they already have their browser window open - rather than clicking the HTML (or HTA) file to cause the browser to launch.
    I used a delay to launch the ResizeTo:

    Window.SetTimeout "Window.ResizeTo 800, 600", 1000 ' (vbscript)
    Window.SetTimeout ( "Window.ResizeTo ( 800, 600)", 1000) /* other */

    The above uses a 1 second (1000 ms) delay; this may be tuneable but you don't always know how slow the users PC is; mine is quite fast.

    PS I am not sure of the exact syntax in the second example, but you get the idea. This totally solves the issue.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 10:15 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.