admin管理员组

文章数量:1397134

I've inherited a website with some funky code. It held together OK until IE 10 when it started to throw this error:

Unable to get property 'offsetHeight' of undefined or null reference Line: 27 Char: 1 Code: 0 UR 

This is the code:

<script type="text/javascript">
function transDiv(){
var theheight=document.getElementById('transcontent').offsetHeight;
document.getElementById('translayer').style.height=theheight+'px';
}
window.onload=transDiv;
setInterval("transDiv()",1); //this handles text resizing through the users browser
</script>

Any thoughts/assistance would be appreciated. Seems to work just fine in Chrome and FF and the client's version of IE (8, ugh).

I've inherited a website with some funky code. It held together OK until IE 10 when it started to throw this error:

Unable to get property 'offsetHeight' of undefined or null reference Line: 27 Char: 1 Code: 0 UR 

This is the code:

<script type="text/javascript">
function transDiv(){
var theheight=document.getElementById('transcontent').offsetHeight;
document.getElementById('translayer').style.height=theheight+'px';
}
window.onload=transDiv;
setInterval("transDiv()",1); //this handles text resizing through the users browser
</script>

Any thoughts/assistance would be appreciated. Seems to work just fine in Chrome and FF and the client's version of IE (8, ugh).

Share Improve this question asked Sep 12, 2013 at 14:33 mary glickmanmary glickman 211 gold badge1 silver badge2 bronze badges 5
  • It means that there's no element on the page whose "id" value is "transcontent". Also that's just a terrible way of doing things; the website must really spin up some fans. – Pointy Commented Sep 12, 2013 at 14:35
  • 2 does document.getElementById('transcontent') return a valid node ? – karthikr Commented Sep 12, 2013 at 14:35
  • Have you tried changing window.onload to document.onload? It's possible that the DOM hasn't been fully loaded/initialised yet. – Matt Fletcher Commented Sep 12, 2013 at 14:36
  • 1 @MattFletcher those are the same events. – Pointy Commented Sep 12, 2013 at 14:37
  • setInterval(…, 1) is much too fast. I also is quite possible that the first invocation happens before document.ready… – Bergi Commented Sep 28, 2014 at 13:34
Add a ment  | 

1 Answer 1

Reset to default 1

The elements don't have time to load in the 1 millisecond interval. You can change the code to this as a workaround to keep the exact same functionality:

function transDiv() {
    var content = document.getElementById('transcontent');
    var layer = document.getElementById('translayer');
    if (!content || !layer)
        return;
    var theheight = content.offsetHeight;
    layer.style.height=theheight+'px';
    window.setTimeout(transDiv, 1);
}
window.onload = transDiv;

However as it appears to be meant to handle resize, just catch the event instead of using timer. Get rid of the window.setTimeout() line and add this below the code:

window.onresize = transDiv;

本文标签: javascriptUnable to get property of undefined or null reference IEStack Overflow