Note that there are some explanatory texts on larger screens.

plurals
  1. POIE document viewport border
    primarykey
    data
    text
    <p>In some versions of IE, there is a thin 2px border surrounding the document view port. I haven't noticed it for any other browsers yet. This poses a slight problem in calculating mouse positions for the page and client areas. Originally, I simply subtracted 2 from each of the calculations to account for the border.</p> <p>But then, when I tested it in different versions of IE and different IE embedding programs, I noticed that in some cases, there is no border. So, simply doing a check for IE and subtracting 2 won't cut it.</p> <p>Is there any way of getting the document view port border in IE?</p> <p><strong>Example1:</strong> finding mouse position inside object</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; var isIE = (!window.addEventListener); window.onload = function(){ var foo = document.getElementById('foo'); if (isIE) foo.attachEvent('onmousemove',check_coords); else foo.addEventListener('mousemove',check_coords,false); } function check_coords(e){ var foo = document.getElementById('foo'); var objPos = getPos(foo); if (isIE) mObj = [window.event.clientX+document.body.scrollLeft-objPos[0], window.event.clientY+document.body.scrollTop-objPos[1]]; else mObj = [e.pageX-objPos[0], e.pageY-objPos[1]]; foo.innerHTML = mObj; } function getPos(obj){ var pos = [0,0]; while (obj.offsetParent){ pos[0] += obj.offsetLeft; pos[1] += obj.offsetTop; obj = obj.offsetParent; } return pos; } &lt;/script&gt; &lt;style&gt; body{ margin:0px; padding:0px; } #foo{ border:3px solid black; position:absolute; left:30px; top:52px; width:300px; height:800px; background-color:yellow; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='foo'&gt;Test test&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>At coordinate [0,0], Internet Explorer (the one's that have the border) returns [2,2]</p> <p><strong>Example2:</strong> getting scrollbar width</p> <pre><code>alert(screen.width-document.body.clientWidth); </code></pre> <p>With a scrollbar width of 17px, Internet Explorer (versions that have the border) returns 21px, not accounting for the 2px borders on each side.</p> <p><strong>UPDATE:</strong><br/> So, I guess it actually <em>was</em> a default style that is applied to the body tag. Sorry guys! I had originally done a <code>document.body.style.borderWidth</code> to check if it was a css style. But I just realized a couple minutes ago, that I should have done <code>document.body.currentStyle['borderWidth']</code>. This returns <code>medium</code>. So, the correct way to get the viewport border <em>without</em> modifying the page (this <strong><em>ONLY</em></strong> applies to IE calculations), you will need to use <code>.currentStyle['borderWidth']</code>. The script appears to work in all other browsers, besides IE, without performing the border check (as far as I have checked). Furthermore, you only have to check for <em>borderWidth</em> in IE- padding or margin don't seem to matter. Also, when subtracting border-widths, make sure you're checking for <code>borderTopWidth</code> AND <code>borderLeftWidth</code>.</p> <p>Anyways, I awarded the bounty to Samuel, since he was the first to mention it might be a default browser style.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload