Basically, I need to draw a <div> over an image. The top and left of the div should match that of the image. This code works in IE but not FireFox. Please help!
Below is the complete code. Just create a 400 X 350 bmp and save as test.bmp.
Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function CreateDiv() {
var imgPosition;
var img = document.getElementById("image");
imgPosition = getElementPosition(img);
var _width=300;
var overLayer = createElement("div");
document.body.appendChild(overLayer);
overLayer.style.position = "absolute";
overLayer.style.background = "red";
overLayer.style.top = imgPosition.top;
overLayer.style.left = imgPosition.left;
overLayer.style.width = _width + "px";
overLayer.style.height = _width + "px";
overLayer.style.border = "1px solid red";
overLayer.style.cursor = "crosshair";
}
function getElementPosition(obj){
var position = new Object();
position.left = calcLeftPosition(obj);
position.top = calcTopPosition(obj);
return position;
}
function calcLeftPosition (obj){
var curleft = 0;
if (obj.offsetParent) {
while (1) {
curleft+=obj.offsetLeft;
if (!obj.offsetParent) {
break;
}
obj=obj.offsetParent;
}
} else if (obj.x) {
curleft+=obj.x;
}
return curleft
}
function calcTopPosition(obj){
var curtop = 0;
if (obj.offsetParent) {
while (1) {
curtop+=obj.offsetTop;
if (!obj.offsetParent) {
break;
}
obj=obj.offsetParent;
}
} else if (obj.y) {
curtop+=obj.y;
}
return curtop;
}
function createElement(element) {
if (typeof document.createElementNS != 'undefined') {
alert("Trying in FireFox");
return document.createElementNS('http://www.w3.org/1999/xhtml', element);
}
if (typeof document.createElement != 'undefined') {
return document.createElement(element);
}
return false;
}
</script>
</head>
<body >
<table cellspacing="0" cellpadding="0">
<tr valign="top">
<td style="width:800px; text-align:center">
<input type="button" value="Create Data" onclick="CreateDiv();" ID="CreateData" NAME="CreateData">
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0">
<tr valign="top">
<td style="width:200px; text-align:center">
</td>
<td style="width:600px; text-align:center">
<img style="cursor:crosshair;" id="image" src="test.bmp">
</td>
</tr>
</table>
</body>
</html>
