JavaScript getElementById(…) is null or not an object IE

为君一笑 提交于 2019-12-02 00:27:10

you do

document.getElementById('documentFrame2').onload = resizeIframe;

before the document is ready so the iframe can be found. call this in the onload- or document-ready-function or in a seperate javascript-block after the iframe.

Try wrapping

document.getElementById('documentFrame2').onload = resizeIframe;

Within:

window.onload = function(){

    document.getElementById('documentFrame2').onload = resizeIframe;
    window.onresize = resizeIframe;
    resizeIframe();
};

The iframe doesn't exist in the DOM until the page/Dom has loaded.

UPDATE:

Didn't see part of the code you posted.

You could keep onload = "resizeIframe" within the element itself and have the JavaScript in the head as:

function resizeIframe() {
    var height = document.documentElement.clientHeight;
    height -= document.getElementById('documentFrame2').offsetTop;
    height -= 20;
    document.getElementById('documentFrame2').style.height = height +"px";
 }


 window.onresize = resizeIframe;

That way you wouldn't need any JavaScript running after the element.

gowtham
  1. alert(document.getElementById('abcId').value);
    
  2. alert(document.formName.elements['abcName'].value);
    

I expirienced the same issue i tried the 2nd one it worked.

If you have used onload"resizeIframe" in the iframe tag, you don't need document.getElementById('documentFrame2').onload = resizeIframe; anymore

I prefer relacing window.onresize = resizeIframe; by:

window.onload = function() {
  window.onresize = resizeIframe;
};

Sometimes the window will be resized before the iframe is ready for operation, and that would cause errors.

========================

edited:

window.onload = function() {
  resizeIframe();
  window.onresize = resizeIframe;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!