0

I'm writing an HTML/JS application that runs under a WebView on Android. Under certain circumstances the page containing the application may not be loaded, or another page might instead (such as 404 page or similar). In general, I need to run a set of JS functions on the application page or take another set of actions in case the application page didn't load.

I'm trying to detect whether the proper page has loaded by detecting whether the appropriate JS is available, but I can't seem to find any way to do it synchronously - only by calling a JS function and expecting a callback via JavasctiptInterface. While this method is feasible, I'm wondering whether there is a better, more elegant and reliable way to accomplish this.

I certainly can't do this by analyzing the page's URL - it might be the right one but with a wrong content.

Any ideas?

Igor K.
  • 583
  • 5
  • 22
  • Why do you consider callback to be not "elegant and reliable way"? – kirilloid Apr 09 '12 at 08:34
  • 'Cause I'd expect from a development platform to provide direct communication interface instead of calling `javascript:...` protocol - that's the sort of things you'd do 6-7 years ago on IE. I honestly expected Android to provide more direct tools to do something as trivial as calling a JS method, but so far I've been unlucky in my search, but I thought maybe I'm looking in the wrong places - hence the question. – Igor K. Apr 09 '12 at 08:45
  • Calling it in from "javascript:..." really looks hacky, but it doesn't look like callback. And why it is not reliable? – kirilloid Apr 09 '12 at 09:33
  • Because I can't be sure just how long will the JS callback take to respond. I could be leaving the user hanging for a while until something happens. I need a way to probe the page's scripts without relying on their integrity. I could do something like: call JS test function -> start timer -> if I get callback before timer ends, the proper JS is alive | if timer ticks then it's not - but **this** is probably the ugliest way to do things. I want to know that there is a better way. – Igor K. Apr 09 '12 at 11:00
  • 2
    Erm.. just check, that function exists from JS: `javascript:typeof someFunc == "function"`. This is time-determined code. – kirilloid Apr 09 '12 at 11:57
  • `javascript:...` will work with spaces in code? But even if it will, how will it give a direct callback? `loadUrl` is `void`. – Igor K. Apr 09 '12 at 13:00
  • Wait, I think I understand. What you mean is to place the callback in that `if` as a case, so it runs immediately? I'll give it a shot. Can you put this as an answer so I can accept it if it works? – Igor K. Apr 09 '12 at 13:02
  • There should be a way in Android, to get result of execution of some js code. You could replace spaces with `%20`. – kirilloid Apr 09 '12 at 13:08
  • kirilloid, can you please add your answer as answer? It worked and I want to accept it. – Igor K. Apr 09 '12 at 14:41

2 Answers2

0

I think you can do it, Please look at PhoneGap project, you will find your answer.

PhoneGap is an awesome free framework for mobile platform.

Sean
  • 1,598
  • 1
  • 11
  • 14
  • PhoneGap is an excellent platform, no question. Unfortunately, the HTML application in my case is already developed and is barely touchable. In any case, the JS-to-Native communication is not the problem here, it's the Native-to-JS without using `.loadUrl('javascript:...)` that bugs me. – Igor K. Apr 09 '12 at 08:37
0
@JavascriptInterface
public void onLoaded(String result) {
    if (result.equals("function")) {
        // defined
    } else {
       // not defined
    }
}

private void checkJSFunction() {
    loadUrl("javascript:yourJSInterfaceName.onLoaded(typeof yourFunctionName)");
}

update: this code will check the JS function define by "typeof" and return its result to java through javascript interface "onLoaded".

superuser
  • 518
  • 5
  • 9
  • Please explain what this code does and how it answers the question. Additionally, it contains a syntax error. – idmean Jul 08 '16 at 06:38