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
1 Answer
Reset to default 1The 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
版权声明:本文标题:javascript - Unable to get property of undefined or null reference IE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744148041a2592928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论