YUI 3.x Home -

YUI Library Examples: Node: Measuring the Window and Document

Node: Measuring the Window and Document

This example shows how to measure the size of the window and document.

Window size
Click me to find out
Document size
Click me to find out

Setting up the HTML

First we need some HTML to work with.

  1. <dl id="demo">
  2. <dt>Window size</dt>
  3. <dd class="yui-data-win">Click me to find out</dd>
  4. <dt>Document size</dt>
  5. <dd class="yui-data-doc">Click me to find out</dd>
  6. </dl>
<dl id="demo">
    <dt>Window size</dt>
    <dd class="yui-data-win">Click me to find out</dd>
    <dt>Document size</dt>
    <dd class="yui-data-doc">Click me to find out</dd>
</dl>

Getting the Dimensions

In this example, we will listen for clicks on the DD elements and update them with screen information.

  1. var onClick = function(e) {
  2. var target = e.target,
  3. h, w;
  4.  
  5. if (target.test('dd')) {
  6. if (target.test('.yui-data-win')) {
  7. h = target.get('winHeight');
  8. w = target.get('winWidth');
  9. } else if (target.test('.yui-data-doc')) {
  10. h = target.get('docHeight');
  11. w = target.get('docWidth');
  12.  
  13. }
  14. target.set('innerHTML', 'width: ' + w + ' height: ' + h);
  15. }
  16. };
  17.  
var onClick = function(e) {
    var target = e.target,
        h, w;
 
    if (target.test('dd')) {
        if (target.test('.yui-data-win')) {
            h = target.get('winHeight');
            w = target.get('winWidth');
        } else if (target.test('.yui-data-doc')) {
            h = target.get('docHeight');
            w = target.get('docWidth');
 
        }
        target.set('innerHTML', 'width: ' + w + ' height: ' + h);
    }
};
 

Adding a Click Listener

Now we just assign the click handler to the DL that will allow us to use event bubbling to handle clicks on each DD.

  1. Y.one('#demo').on('click', onClick);
Y.one('#demo').on('click', onClick);

Full Script Source

  1. YUI().use('node', function(Y) {
  2. var onClick = function(e) {
  3. var target = e.target,
  4. h, w;
  5.  
  6. if (target.test('dd')) {
  7. if (target.test('.yui-data-win')) {
  8. h = target.get('winHeight');
  9. w = target.get('winWidth');
  10. } else if (target.test('.yui-data-doc')) {
  11. h = target.get('docHeight');
  12. w = target.get('docWidth');
  13.  
  14. }
  15. target.set('innerHTML', 'width: ' + w + ' height: ' + h);
  16. }
  17. };
  18.  
  19. Y.one('#demo').on('click', onClick);
  20. });
YUI().use('node', function(Y) {
    var onClick = function(e) {
        var target = e.target,
            h, w;
 
        if (target.test('dd')) {
            if (target.test('.yui-data-win')) {
                h = target.get('winHeight');
                w = target.get('winWidth');
            } else if (target.test('.yui-data-doc')) {
                h = target.get('docHeight');
                w = target.get('docWidth');
 
            }
            target.set('innerHTML', 'width: ' + w + ' height: ' + h);
        }
    };
 
    Y.one('#demo').on('click', onClick);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings