Results 1 to 3 of 3

Thread: How do i detect page direction

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do i detect page direction

    Hi all,

    I noticed that when the 'direction' is set to an external css file, it's not possible to detect page's 'direction' property via javascript. It returns NULL

    Code:
        var elm=document.getElementsByTagName('body');
    
        if(elm[0].dir=='rtl'||elm[0].style.direction=='rtl')
            return true;
        else
            return false;
    any suggestions?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Taken loosely from:

    http://www.quirksmode.org/dom/getstyles.html

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    body {
    	direction: rtl;
    }
    </style>
    <script type="text/javascript">
    function getStyle(el,styleProp)
    {
    	var x = document.getElementById(el) || document.body;
    	if (x.currentStyle)
    		var y = x.currentStyle[styleProp];
    	else if (window.getComputedStyle)
    		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
    	return y;
    }
    onload = function(){alert(getStyle(null, 'direction'));};
    </script>
    </head>
    <body>
    Hi
    </body>
    </html>
    Notes: This will even work if the direction hasn't been set, returning the default (ltr), or if it is set using the dir attribute, returning whatever the computed style direction is for the element. For style properties that contain hyphens, the camel style notation would be required for half of the getStyle function. So, if you intend to use this for other properties (direction is safe because it's all one word) you may have to deal the the camel/hyphen issue.
    Last edited by jscheuer1; 03-04-2010 at 01:14 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John, many thanks for you it's really helped me

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •