3

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!!!!

4
  • 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 setting dataType: "script" of ajax request? Commented Dec 2, 2013 at 16:12
  • use ` to wrap code in comment Commented Dec 2, 2013 at 16:24
  • ty for reply! here's the relevant code: 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... Commented Dec 2, 2013 at 16:27
  • I tried also with var script = $('<script [...]>' + '</' + 'script>'); without success :/ Commented Dec 2, 2013 at 16:41

1 Answer 1

1

I tried the solution found in this thread:

jQuery getScript not working in Android WebView when using Local Assets/HTML/Resources

and it worked!

I report the solution:

var scriptUrl = "test.js";
var head = document.getElementsByTagName("head")[0];
script = document.createElement('script');
script.type = 'text/javascript';
script.src = scriptUrl;
script.onload = function() { alert("Success"); };
script.onerror = function(e) { alert("failed: " + JSON.stringify(e)); };
head.appendChild(script);

So if i use the jquery append() method it doesn't work, but if i use the javascript appendChild method it work...

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for onload, onerror syntax.. its so tough to find help for javascript methods.. damn eclipse does not have autocomplete of methods also during development.. its like developing in fotran 30 years back..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.