I seem to have found an issue with Internet Explorer 8, when finding the position of a Text Range - For instance the currently selected text. I have not found been able to find any reports of this issue on StackOverflow or elsewhere.
TextRange.offsetLeft and TextRange.offsetTop report the left and top corner of the range, and in all cases I have seen in IE8, they are roughly correct, except in the case when the range is within an IFrame. When the range is within an IFrame the values for offsetLeft and offsetTop are shifted by a negative amount relative to the position of the IFrame within it's parent. (See example below)
This issue appears only when:
- The browser is IE8
- The page is in standards mode
This issue does not appear when:
- The browser is IE7 or IE10
- The page is in quirks mode
My Questions:
- Can others confirm this issue or am I crazy?
- Is this a known issue?
- Are there any sane solution or work arounds? (A sane solution would be one where the JS in the frame doesn't need to know anything about its parent window)
Thanks.
An example of the issue: (See the difference in IE8 vs. IE9)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>IE8 IFrame Text Range Position Test Page</title>
<style type="text/css">
body {
font-family: Tahoma;
}
#target {
background-color: #CCC;
position: absolute;
left: 50px;
top: 50px;
}
#bullsEye {
position: absolute;
background-color: red;
width: 5px;
height: 5px;
}
iframe {
margin: 10px 75px;
}
</style>
<script type="text/javascript" charset="utf-8">
function target() {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById('target'));
range.select();
var bullsEye = document.createElement('div');
bullsEye.id = 'bullsEye';
bullsEye.style.left = range.offsetLeft + 'px';
bullsEye.style.top = range.offsetTop + 'px';
document.body.appendChild(bullsEye);
document.getElementById('output').innerHTML = 'range.offsetLeft = ' + range.offsetLeft + ', range.offsetTop = ' + range.offsetTop;
}
</script>
</head>
<body>
<div id="target">Target</div>
<input type="button" value="Hit Target" onclick="target();"> <span id="output"></span>
<br><br><br><br><br>
<script>
if (window.parent == window) document.write('<iframe src="?tfr" height="150" width="500"></iframe>');
</script>
</body>
</html>