Skinny
Skinny

Reputation: 91

PhoneGap 2.9.0 Loading External Scripts

Preface: I'm using Coffeescript and Haml and testing on my Android phone using PhoneGap build.

I am attempting to load an external script that is being compiled on the server for my phonegap app. When testing the page locally everything works but when testing on the actual device it doesn't seem to be getting the script. I have tried loading the script normally in the index.html:

%script{:type => 'text/javascript', :src => "http://192.168.5.112:3000/assets/mobile.js"}

I have also tried using jQuery's getScript, get, and ajax calls:

$.getScript('http://192.168.5.112:3000/assets/mobile.js', alert('Success'))

$.get 'http://192.168.5.112:3000/assets/mobile.js', (data) -> alert 'Success server' + data

$.ajax 'http://192.168.5.112:3000/assets/mobile.js',
  error: (jqXHR, textStatus, errorThrown) ->
    alert('localError: ' + jqXHR + textStatus + errorThrown)
  success: (data, textStatus, jqXHR) ->
    alert('localSuccess: ' + data + textStatus + jqXHR)

All of which give me the successful alert but still isn't loading the script. I also have whitelisted the server in the config.xml

<access origin="http://192.168.5.112:3000" subdomains="true" />

Still no luck. I am using weinre to the best of its ability to debug the issue but either the script doesn't show up at all in the network panel or it is shown there but with no content.

Any ideas are greatly appreciated!

Upvotes: 1

Views: 1135

Answers (1)

Dom
Dom

Reputation: 2569

192.168 is a local-host IP. When you run it on your PC it is looking on your PC for the file. When you run it on the phone it is looking on the phone for the file. That will not work. Always download the JS file and include it in your project. If you include it in your project under a /js folder then include it in the head tag of your HTML as follows:

<script src="js/myScript.js"></script>

No need to whitelist domains or anything.

If the JS file is publicly accessible (ie: anyone, not just you, can access it via http://) then you can use:

<script src="http://www.serverWhereTheScriptIs.com/myScript.js"></script>

Attempting to download the script (example 2) can have other issues though. For example, lets say a user wants to use your app but doesn't have internet connectivity. The app will puke all over itself. Even if they do have connectivity, it increases the load time of the app. It is always better to have a larger app because you included a JS library than it is to have a slower/inoperable app.

Upvotes: 2

Related Questions