How to wait 2 times for the complete loading of a document?

1

I have a document, which is loaded twice :

  • Upload the required .js files.
  • From one of those files, I set the href of the style sheet to use.
  • When I finish loading the style sheet it's when I want to paint on the screen.
  • The goal is to avoid the ugly effect of applying the styles after displaying the page; The design change is clearly appreciated.

    Currently, I do it like this:

      

    index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <link id="SKIN.CSS" rel="stylesheet"/>
    <script type="text/javascript" charset="UTF-8" src="lib/spin.min.js"></script>
    <script id="APP.JS" type="text/javascript" charset="UTF-8"></script>
    <title></title>
    </head>
    <body>
    <div id="FSAUX" style="height:1em;width:1em;left:100%;position:fixed;top:100%;"></div>
    <div id="CMAUX" style="height:1cm;width:1cm;left:100%;position:fixed;top:100%;"></div>
    <script>
    window.Gui = { };
    
    window.App = {
      $Spinner: new Spinner( )
    };
    
    App.$Spinner.spin( document.body );
    
    ( function( ) {
      var curr = window.location.href,
          url = 'app.php?sw=' + screen.width.toString( ) + '&sh=' + screen.height.toString( ),
          element = document.getElementById( 'FSAUX' );
    
        curr = curr.substring( 0, ( curr.indexOf( '#' ) == -1 ) ? curr.length : curr.indexOf( '#' ) );
        //this removes the query after the file name, if there is one
        curr = curr.substring( 0, ( curr.indexOf( '?' ) == -1 ) ? curr.length : curr.indexOf( '?' ) );
        //this removes everything before the last slash in the path
        curr = curr.substring( curr.lastIndexOf( '/' ) + 1, curr.length );
    
        url += '&fw=' + element.offsetWidth.toString( );
        url += '&fh=' + element.offsetHeight.toString( );
    
        element = document.getElementById( 'CMAUX' );
        url += '&cw=' + Math.round( screen.width / element.offsetWidth ).toString( );
        url += '&ch=' + Math.round( screen.height / element.offsetHeight ).toString( );
    
        url += '&file=' + curr;
    
        document.getElementById( 'APP.JS' ).src = url;
    } )( );
    </script>
    <script type="text/javascript" charset="UTF-8" src="lib/webix_debug.js"></script>
    <script type="text/javascript" charset="UTF-8" src="lib/i18n/es.js"></script>
    <script>
    webix.ready( function( ) {
      webix.i18n.setLocale( 'es-ES' );
      webix.ui.fullScreen( );
    
      App.Load( );
    } );
    </script>
    </body>
    </html>
    

    From there, I call the true application, generated in PHP . The code that really arrives to the browser is

    App.Load = function( ) {
      document.getElementById( 'SKIN.CSS' ).href = 'http://cdn.webix.com/4.3/webix.css';
      webix.ready( App.Run );
    }
    
    App.Run = function( ) {
      'use strict';
    
      App.$Routes = {
        'home': App.Home
      }
    
      window.onbeforeunload = onUnload;
    
      // Prevenimos el botón 'back'.
      if( ( 'history' in window ) && ( 'pushState' in window.history ) ) {
        onNavigateBack( );
        window.addEventListener( 'popstate', onNavigateBack );
      }
    
      Gui.Init( );
    
      // ... Resto del código ...
    

    As you can see, there are 2 calls at webix.ready( ) . One in the index.html , from where I call App.Load( ) when finishing loading the dependencies of the first one.

    Once in App.Load( ) , I set the href , and I call back to webix.ready( ) , to launch the true start function: App.Run( ) . This scheme does not work . App.Run( ) is called before to load the styles.

    I have also tried a pedreste version of the above:

    App.Load = function( ) {
      document.getElementById( 'SKIN.CSS' ).href = 'http://cdn.webix.com/4.3/webix.css';
    
      setTimeout( loaded, 0 );
    
      function loaded( ) {    
        if( document.readyState === 'complete' ) {
          setTimeout( App.Run, 0 );
        } else
          setTimeout( loaded, 10 );
      }
    }
    
    App.Run = function( ) {
      'use strict';
    
      App.$Routes = {
        'home': App.Home
      }
    
      window.onbeforeunload = onUnload;
    
      // Prevenimos el botón 'back'.
      if( ( 'history' in window ) && ( 'pushState' in window.history ) ) {
        onNavigateBack( );
        window.addEventListener( 'popstate', onNavigateBack );
      }
    
      Gui.Init( );
    
      // ... Resto del código ...
    

    Apparently, webix.ready( ) can only be called 1 time per document. In the pedreste version, it seems that the status of document.readyState does not change after completing the full load the first time.

    How do I wait for the document to load completely 2 times ?

        
    asked by Trauma 27.05.2017 в 10:08
    source

    1 answer

    2

    If the goal is to know when the CSS is loaded to execute the start codes, you could substitute the <script> of your index.html with this code:

    window.onload = function(){
      // Cuando carge tu pagina
      webix.i18n.setLocale( 'es-ES' );
      webix.ui.fullScreen( );  
      
      // Se añade el elemento CSS
      var hojaCss = document.createElement('link')
      hojaCss.rel = 'stylesheet'
      hojaCss.type = 'text/css'
      hojaCss.link = 'http://cdn.webix.com/4.3/webix.css'
      document.getElementsByTagName('head').appendChild(hojaCss)
      
      // Cuando carge el css
      hojaCss.onload = function(){
       App.Run()
      }
    }

    This establishes what you need:

  • Load the HTML first, JS.
  • After the load the css is added and loaded.
  • When the CSS is loaded, execute App.Run()
  • answered by 27.05.2017 / 12:10
    source