Can we hide javascript loading from the user?

前端 未结 2 1106
时光取名叫无心
时光取名叫无心 2021-01-25 23:35

If I have a page which gets generated using PHP and then modified using JS which runs using:

window.onload=myFunction;

Then the user sees the origi

相关标签:
2条回答
  • 2021-01-26 00:07

    I would recommend first settings some class on the body which will toggle the visibility of some elements.

    <body class="loading"> .... </body>
    

    Then, when the script finishes loading, you can remove this class and show all the things you need. You can even use the "loading" class to show some fancy background, like a spinning circle of dots in Mozilla.

    Also some projects use another method of loading scripts instead of "onLoad". First you create a global array, let's say, named "domReady".

    window.domReady = [];
    

    And when you need to add some function that will be run on page load, you just push it into this array:

    window.domReady.push( function foo(){ alert('Hey!'); } );
    

    And at the last line of the document you put the function that will run all the pushed procedures:

    for (procedure in window.domReady) {
        window.domReady[procedure].call();
    }
    

    Since it will be the last line of the document before the closing tag, the DOM will be ready, and you won't have to wait until all the images are loaded.

    0 讨论(0)
  • 2021-01-26 00:28

    OK I found this little nugget of jquery which works a treat:

    $(document).ready(function() {
      //Call your function here
    });
    
    0 讨论(0)
提交回复
热议问题