DHTML: get updated item size

I am updating a srctag attribute <img>in javascript. After I have done this, I need to get a new parent height <div>so that I can update the size of other elements on the page. If I get the parent property offsetHeightright after updating the image src, however, I get the height before the change was made. Is there a way to get the browser to display an updated page so that I can get a new height?

Repro:

<html>
<head>
<script type="text/javascript">
    function changeImage(){
        document.getElementById("image").src = "b.jpg";
        // here I need to re-render the page
        alert(document.getElementById("parent").offsetHeight);
        // ^ alerts the height of a.jpg, not b.jpg (unless this is the second click)
    }
</script>
</head>

<body onclick="changeImage()">

<div id="parent">
    <img id="image" src="a.jpg" />
</div>

</body>
</html>
+3
source share
2 answers

, . "src" (b.jpg) , "onload" , .

+2

, Marc. , , , b.jpg. , , , setTimeout ('alert (document.getElementById( "parent" ). OffsetHeight);', 1); .

+3

All Articles