JavaScript event [removed] not triggered

后端 未结 8 1047
无人及你
无人及你 2021-02-01 01:15

I\'ve got the following code in a website:

 window.onload = resize;
 window.onresize = resize;

 function resize(){
  heightWithoutHeader = (window.innerHeight -         


        
8条回答
  •  野的像风
    2021-02-01 01:34

    Move the window.onload line to the end of the javascript file or after the initial function and it will work:

    function resize(){
        heightWithoutHeader = (window.innerHeight - 85) + "px"; 
        document.getElementById("main-table").style.height = heightWithoutHeader;
        document.getElementById("navigation").style.height = heightWithoutHeader;
    }
    // ...
    // at the end of the file...
    window.onload = resize;
    window.onresize = resize;
    

    But it's a best practice if you don't replace the onload too. Instead attach your function to the onload event:

    function resize(){
        heightWithoutHeader = (window.innerHeight - 85) + "px"; 
        document.getElementById("main-table").style.height = heightWithoutHeader;
        document.getElementById("navigation").style.height = heightWithoutHeader;
    }
    // ...
    // at the end of the file...
    window.addEventListener ? 
        window.addEventListener("load",resize,false) 
        : 
        window.attachEvent && window.attachEvent("onload",resize);
    

    That worked for me and sorry for my english.

提交回复
热议问题