JavaScript: Get current position

by MythOfEchelon

HTML

<!DOCTYPE html> <!-- This is just my HTML5-valid template. Make sure you set this up correctly for your personal needs -->

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        
        <style>
            * {
                margin: 0;
                padding: 0;
            }
        
            html {
                font: 16px "Segoe UI", "Segoe WPC", Helvetica, Arial, "Arial Unicode MS", Sans-Serif;
            }
            
            div {
                float: left;
            }
            
            #page_Wrapper {
                
            }
            
            #elem {
                background-color: red;
                width: 200px;
                height: 100px;
                margin-top: 100px;
                margin-left: 300px;
            }
        </style>
    </head>
    
    <body>
        <div id="page_Wrapper">
            <div id="elem" onLoad="alert('someht');">
                
            </div>
        </div>
        
        <!-- Include the script after all DOM elements have been created, to avoid any potential DOM-related errors -->
        <script>
            window.onLoad = getPosition();
            
            function getPosition(){
                var element = document.getElementById("elem");
                
                var pos_Left = 0;
                var pos_Top = 0;
                
                if (element.offsetParent){
                    do {
                        pos_Left += element.offsetLeft;
                        pos_Top += element.offsetTop;
                    } while (element = element.offsetParent);
                    
                    alert(pos_Left + " " + pos_Top);
                }
            }
        </script>
    </body>
</html>