Search code examples
javascriptcordovaextjssencha-touch-2

loading app.js in deviceReady sencha touch cordova app


I have an issue I'm using device APIs the problem is device.uuid can be available after document ready and the sencha touch load app.js before document be ready so what I get when I use device.uuid in sencha code it appear null

tried to call this function by

<body onload="allJs();">

<script>
    function allJs(){           
                document.write('<script src="all.js"><\/script>');
            }
</script>

if I put document .write before the on load function sencha app load perfectly without device.uuid

<body onload="allJs();">

<script>
    document.write('<script src="all.js"><\/script>');
    function allJs(){           
                //document.write('<script src="all.js"><\/script>');
            }
</script>

what should I do


Solution

  • you can get script asynchronously by using this functions

    function lazyload() {
        var scriptTag = document.createElement('script'); 
        scriptTag.src = "//my_example.js"; // set the src attribute
        scriptTag.type = 'text/javascript'; // if you have an HTML5 website you may want to comment this line out
        scriptTag.async = true; // the HTML5 async attribute
        var headTag = document.getElementsByTagName('head')[0];
        headTag.appendChild(scriptTag);
    }
    

    and for phonegap or cordova ready use

    document.addEventListener('deviceready', function(){
            lazyload();
    }, false);
    

    you can use jQuery getScript function or create it by yourself, see the links in sources

    sources :

    https://chris.lu/article/read/506de698268c420f0d000004

    http://jeremyhixon.com/snippet/loading-javascript-files-asynchronously/