I have an Android application that consist in a simple WebView that load a local html file in the assets folder of the project.
The HTML has a tag that calls (AJAX) an external service and expects as a response a String that represent a .js filename (say, 'test.js').
In test.js there is a simple funcion declaration, as:
var testFunction = function(){
// some code here
}
The AJAX callback then construct via javascript a tag that points to the .js file and append it to the head of the document; then call the testFunction:
$.ajax('externalService').done(function(response){
var script = // construct the script tag;
$('head').append(script);
testFunction();
});
Important: the script tag points to an external .js file, like
<script src="http://justatest.com/test.js">
... and all works fine!
Now i try to do the same thing putting the test.js inside the assets folder of the project. Obviously i changed the src of the script created in the callback with a relative path:
<script src="test.js"></script>
but when i try to invoke the function testFunction i get an error (testFunction is not defined).
The local path is correct (i put jquery.js in the same local folder of test.js and loaded it directly in the html with a with no errors).
I put all the attributes (like type="text/javascript") in the tag as well...
so, why the webview doesn't load a local .js file in this way?
thank you :)
EDIT: I tried the solution found in this thread:
jQuery getScript not working in Android WebView when using Local Assets/HTML/Resources
and it worked!!!!
var script = // construct the script tag;Could you post this relevant code? How do you construct your script tag? BTW, why not using getScript() jquery's method or settingdataType: "script"of ajax request?var script = $('<script type="text/javascript" src="http://justatest.com/test.js"/>');for the local file the src is just "test.js". i didn't know the getScript() jquery's method :) i'll take a look. Also, i can't use the dataType: 'script' for the ajax request, because the response is a JSON indeed, with some info and a property that is a string with the js name. However, all works fine when i point an external js file... the problem is only when i point to the local file...var script = $('<script [...]>' + '</' + 'script>');without success :/