RicksWorking
Programmer
I am trying to write some javascript that will, when passed a reference to a div, will hide that div and replace it with another div of the same dimensions and in the same position.
For example.
I have a div that contains some content.
The user interacts with this content and results in the content being updated by AJAX.
While the content is being refreshed, I want to replace the content with a 'loading' div.
I have written a function to do this and I successfully hide the first div and create a second div with correct dimensions. However, I am having trouble placing the second div in the same location as the original div.
What am I doing wrong??
I have included my code below:
function lockScreen(criteria)
{
var targetDiv = document.getElementById(criteria);
var div = document.createElement('div');
div.setAttribute('id',criteria +"_lock");
var image = document.createElement('image');
image.setAttribute('src','images/loading.gif');
div.appendChild(image);
div.style.width=targetDiv.offsetWidth + "px";
div.style.height=targetDiv.offsetHeight+ "px";
div.style.width = finPosX(targetDiv) + "px";
div.style.height=findPosY(targetDiv) + "px";
alert(div.style.left);
alert(div.style.top);
div.style.background="#ededed";
div.style.zIndex=-100;
document.body.appendChild(div);
targetDiv.style.visibility="hidden";
}
An example use of this might be:
<script>
lockScreen("myDiv");
</script>
<div id="myDiv">
LOTS OF CONTENT
</div>
Note that the class above uses the two methods below:
function findPosX(obj)
{
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function findPosY(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}
These I got from quirksmode.org
Many thanks for any help!
For example.
I have a div that contains some content.
The user interacts with this content and results in the content being updated by AJAX.
While the content is being refreshed, I want to replace the content with a 'loading' div.
I have written a function to do this and I successfully hide the first div and create a second div with correct dimensions. However, I am having trouble placing the second div in the same location as the original div.
What am I doing wrong??
I have included my code below:
function lockScreen(criteria)
{
var targetDiv = document.getElementById(criteria);
var div = document.createElement('div');
div.setAttribute('id',criteria +"_lock");
var image = document.createElement('image');
image.setAttribute('src','images/loading.gif');
div.appendChild(image);
div.style.width=targetDiv.offsetWidth + "px";
div.style.height=targetDiv.offsetHeight+ "px";
div.style.width = finPosX(targetDiv) + "px";
div.style.height=findPosY(targetDiv) + "px";
alert(div.style.left);
alert(div.style.top);
div.style.background="#ededed";
div.style.zIndex=-100;
document.body.appendChild(div);
targetDiv.style.visibility="hidden";
}
An example use of this might be:
<script>
lockScreen("myDiv");
</script>
<div id="myDiv">
LOTS OF CONTENT
</div>
Note that the class above uses the two methods below:
function findPosX(obj)
{
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function findPosY(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}
These I got from quirksmode.org
Many thanks for any help!