0

I am writing a Mobile App (PhoneGap) and want the ability to either run local JS file or download a new version of a JS file and use this version.

In the first instance, I am trying to set the 'src' attribute of the script object, which I can see it's work (using fiddler). But though I have loaded the script, I cannot access any of the objects, not sure why though.

My Code:

    function LoadApplicationJs() {
        var AppJs = localStorage.getItem("ApplicationJs");

        if (AppJs == null) {
            $("#Application").attr("src","Application.js");


        }
    }

 LoadApplicationJs();
 alert(app);

Any ideas?

Thanks Paul.

Paul
  • 65
  • 1
  • 8

3 Answers3

1

Have you consider using Jquery getScript it loads a JavaScript file from the server and the script is executed in the global context.

http://api.jquery.com/jquery.getscript/

From Jquery samples:

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

Recently a similar question was posted where requirejs was better suited for the task in case it also helps:

RequireJS Demo

Community
  • 1
  • 1
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • Thank you but I think it's more a timing issue, not sure, I have load the JS file, I can see that it's been loaded but cannot access the app object. – Paul Jan 28 '14 at 22:17
  • By simply adding a file to your html it won't execute it. In case $.getScript they use the source of the file and execute it by using jQuery.globalEval( text ); if you don't want to use JQuery your options is to use the eval function but you will need to get the source and repeat what JQuery is doing – Dalorzo Jan 28 '14 at 22:22
  • Here is the source code if you want: https://github.com/jquery/jquery/blob/master/src/ajax/script.js – Dalorzo Jan 28 '14 at 22:23
  • Thank you but, I can load the script, not a problem. What is happening is that I load the script then trying access the objects within the script but returns undefined. I now know that the JS is being loaded/evaluated after I am trying to access the objects. – Paul Jan 28 '14 at 22:33
0

You can also use RequireJS to load and eval your javascript http://requirejs.org

require(["helper/util"], function(util) {
    //This function is called when scripts/helper/util.js is loaded.
});
Usse
  • 66
  • 5
0

not sure it helps, but some devices requires strickter javascript. one time it happened to me when my client tested an app on his device and it crashed, after it worked on mine. the problem was a function which returned a different function(return f()) which was suppose to return a string. allas, before i wrote return f()+"" it crashed when i tried to access string attributes like length.
if you can specify what version of phonegap and OS your'e working on, and on which device, it might help. if you could post the code that you manage to load and doesnt work, instead of the code calling it that you say there is nothing wrong with, it would help even more.

Yuval Perelman
  • 4,499
  • 1
  • 22
  • 32
  • The script failed on the device. Phonegap 3.1 but worked on my browser. Either way given up, don't have time. – Paul Jan 30 '14 at 05:05