I can't check for errors

I have an error in my code but I don’t know how it can be solved, can you help me to solve it?
I’m using JavaScript.
The code is as follows (Note: This code is realized by running the code in arg.code in the iframe sandbox environment. arg.code is the code in string format passed in):

const iframe = document.createElement(“iframe”);

    iframe.src = "about:blank";

    iframe.style.display = "none";

    document.body.appendChild(iframe);

    const ctx = iframe.contentWindow.document || window.frames[0].document;

    window.onload = function() {

      if (ctx.open) {

        ctx.open();

        if (ctx.write) {

          ctx.write(arg.code);

        }

        ctx.close();

      }

    };

    iframe.parentNode.removeChild(iframe);