html - Javascript Cannot read property 'parentElement' of null - Stack Overflow

I've search the forum for why my img is null when getElementById is called, and I've moved th

I've search the forum for why my img is null when getElementById is called, and I've moved the script below the img, but I am still getting the error that the image is null.

Here is my code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img id="irimg" src="ir.bmp" alt="Video Stream" style="width:640px;height:480px;">
<script type="text/javascript">
function loadNextImage() {
    // An image element for loading our next image
    var ourImage = new Image();

    // Setup the event handlers before setting the source
    ourImage.onload = function() {
        // Request the next frame
        requestAnimationFrame(loadNextImage);

        // Swap out the image element on the page with the new one
        var oldImage = document.getElementById('irimg');
        oldImage.parentElement.insertBefore(ourImage, oldImage);
        oldImage.parentElement.removeChild(oldImage);
    };

    ourImage.onerror = function(){
        // Something went wrong with this frame. Skip it and move on.
        requestAnimationFrame(loadNextImage);
    }

    // Finally load the image
    ourImage.src = "ir.bmp?" + new Date().getTime();
};

requestAnimationFrame(loadNextImage);
</script>
</body>
</html>

The error is occurs here:

var oldImage = document.getElementById('irimg');
oldImage.parentElement.insertBefore(ourImage, oldImage);

The error given by Chrome is Uncaught TypeError: Cannot read Property 'parentElement' of null.

I'd appreciate help in understanding why it is null. Thanks.

I've search the forum for why my img is null when getElementById is called, and I've moved the script below the img, but I am still getting the error that the image is null.

Here is my code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img id="irimg" src="ir.bmp" alt="Video Stream" style="width:640px;height:480px;">
<script type="text/javascript">
function loadNextImage() {
    // An image element for loading our next image
    var ourImage = new Image();

    // Setup the event handlers before setting the source
    ourImage.onload = function() {
        // Request the next frame
        requestAnimationFrame(loadNextImage);

        // Swap out the image element on the page with the new one
        var oldImage = document.getElementById('irimg');
        oldImage.parentElement.insertBefore(ourImage, oldImage);
        oldImage.parentElement.removeChild(oldImage);
    };

    ourImage.onerror = function(){
        // Something went wrong with this frame. Skip it and move on.
        requestAnimationFrame(loadNextImage);
    }

    // Finally load the image
    ourImage.src = "ir.bmp?" + new Date().getTime();
};

requestAnimationFrame(loadNextImage);
</script>
</body>
</html>

The error is occurs here:

var oldImage = document.getElementById('irimg');
oldImage.parentElement.insertBefore(ourImage, oldImage);

The error given by Chrome is Uncaught TypeError: Cannot read Property 'parentElement' of null.

I'd appreciate help in understanding why it is null. Thanks.

Share Improve this question edited Nov 2, 2016 at 12:45 dashdashzako 1,34615 silver badges24 bronze badges asked Nov 2, 2016 at 11:46 PhilBotPhilBot 6020 gold badges91 silver badges190 bronze badges 3
  • 1 Using Chome's debug mode (F12), set a breakpoint on the line oldImage.parentElement.insertBefore(ourImage, oldImage);, move the cursor on top of oldImage and see what is shown (tooltip-style pop). – FDavidov Commented Nov 2, 2016 at 11:50
  • 1 It could be that the script is running before the page is fully loaded, meaning the iamge hasn't rendered yet. Normally if you have a script that accesses DOM elements you want to make sure that they have been loaded into the DOM first before you try accessing them. Make your script run once the page has loaded, rather than immediately – Jayce444 Commented Nov 2, 2016 at 11:50
  • 1 Agree with @Jayce444 as a possible source. Exercising what I suggested may give you some hints. – FDavidov Commented Nov 2, 2016 at 11:52
Add a ment  | 

1 Answer 1

Reset to default 3

You're removing the image with that ID, and then next time your code es through it can't find any image with that ID.

You need to set that ID on your new image that you're replacing the old one with.

like so

// An image element for loading our next image
var ourImage = new Image();
ourImage.id='irimg';

see: https://jsfiddle/9Lp1724v/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419074a4440318.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信