Question

I'm hitting a very peculiar difference between the Android browser and a WebView. Namely, I have the following markup:

<script type="text/javascript">
    <!--

    // some js code...

    // -->
</script>

Now, in the browser, this works perfectly fine. However, when loading the markup in the WebView using loadData(markup, "text/html", "utf-8"), the JavaScript code is not executed at all.

Now, if I remove the HTML comment tags (<!-- and -->), it works fine in the WebView as well. However, this is not something that I want to do, because the markup is coming from a web service that I'd rather not change.

Is there something I can do with the WebView to accept this markup?

Was it helpful?

Solution

On the whole, the put-the-JavaScript-in-comments trick appears to be no longer the recommended pattern, and so I'd encourage you to reconsider your plan to keep serving it that way.

You have obviously enabled JavaScript in the WebView, otherwise it would not work when you tried removing the comments. I know of no other setting to tell WebView to to ignore the comment markers.

You're welcome to examine the AOSP Browser code to try to find out what they do, though that code is nasty, brutish, and long. You could patch up the HTML before handing it to the WebView. You could set a particular user agent on the WebView and have your Web service hand back cleaned-up HTML for that user agent, leaving the rest of your Web service clients unaffected. And you can see if you get a response to your bug report, though there's no assurance of getting such a response.

Beyond that, I'm out of ideas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top