0

I am using MS Edge in IE11 compatibility mode and have an HTML page in which I have:

<input id="onLoadAttributeHiddenId" type="hidden" value="schadenAuswahl.setCustomCheck();schadenAuswahl.createArrays();"> 

Further below, I have also:

<script language="JavaScript1.1" src="../js/table_head_uip.js"></script>

And in the table_head_uip.js, I have:

document.body.addEventListener('load', customOnLoadFunction, false);

function customOnLoadFunction(){
    var onLoadAttributeFunctStr = document.getElementById("onLoadAttributeHiddenId").value;

    var onLoadAttributeFunct = new Function(onLoadAttributeFunctStr);
    onLoadAttributeFunct; 
}

Now, when I put breakpoints in the table_head_uip.js, the line.

   var onLoadFunct = document.body.addEventListener('load', customOnLoadFunction, false);

It gets executed, but the function customOnLoadFunction never gets completed. Also, I do not receive any errors in the console.

Any help will be appreciated.

Alex Mi
  • 1,409
  • 2
  • 21
  • 35
  • 2
    Note that `.addEventListener()` always returns `undefined`. Also, in `customOnLoadFunction()`, you don't call that function you construct; it needs `()` after the reference to be a function call. – Pointy Nov 29 '22 at 15:52
  • 2
    Also there's no need for the `language` attribute. – Pointy Nov 29 '22 at 15:52
  • I am sorry, I did not get your second point. But I corrected my question, so that hopefully your first point should be now OK. Could you please elaborate on what exactly did you mean? – Alex Mi Nov 29 '22 at 16:08

1 Answers1

3

load events only fire on elements which load things (like an <img> with a src attribute) and the window object (for when the entire document, including its dependencies, has loaded).

The body element doesn't load anything.

The equivalent of the obsolete onload attribute for the body is a load event that fires on the window object. That attribute only existed because, since window isn't an element, you couldn't put attributes on it.


Additionally, the statement onLoadAttributeFunct doesn't do anything. To be useful you need to, for example, put () after it to call it.

new Function(onLoadAttributeFunctStr) is effectively a time delayed eval and is considered dangerous, slow, and hard to debug. You should reconsider the design of your application if you need that.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you very much for your response and especially for "the statement onLoadAttributeFunct doesn't do anything" – Alex Mi Nov 30 '22 at 09:17