JQuery.getScript () ... what am I doing wrong?

I am programming an iPhone application using Phonegap. I have local .html and .js files. My index.html file contains the following:

 function onBodyLoad() { document.addEventListener("deviceready", deviceReady, false); } function deviceReady() { $.getScript("js/order.js"); } 

I researched and researched, but just can't understand why my "order.js" file is not called by the $.getScript method. Any ideas? Or is there another way to call this .js file inside the deviceReady function in my index.html ?

+4
source share
1 answer

For me, the following solution worked very well.

  • Add a custom jQuery function that uses ajax and caches the loaded script:

     function init() { // Create a custom cached script importer based on ajax jQuery.cachedScript = function(url, options) { // Allow custom options, but dataType, cache and url are always predefined options = $.extend(options || {}, { dataType: "script", cache: true, url: url }); return jQuery.ajax(options); }; importScripts(); } 
  • Import the scripts and possibly handle done and fail :

     function importScripts() { $.cachedScript("js/events.js") // Wait for the script to be loaded, before adding the listener .done(function() { document.addEventListener("deviceready", onDeviceReady, false); }); $.cachedScript("js/navigation.js"); $.cachedScript("js/mark.js"); } 

Here it is :) More information can be found here .

0
source

Source: https://habr.com/ru/post/1390740/


All Articles