android - How to find out if WebView is displayedonscreenvisible using JavaScript - Stack Overflow

I have a webpage that "plays a video" using sprite sheets. The page is mobile-optimized, so i

I have a webpage that "plays a video" using sprite sheets. The page is mobile-optimized, so it can get loaded into Android and iOS WebViews. I'd like to know is when the page is visible so only after that I can play the video. I don't want users to catch the video mid-stream because the WebView lags in presenting itself.

I can see some developers might wait until the whole page has finished pulling in all the assets from the page before making it visible to the user. So, I don't want the "video" to start before that time. I can't rely on window.onload because that event fires even when the WebView isn't onscreen or visible.

How can I acplish this from the client side, with some JavaScript, preferably?

[Edit] To be clear, I'm implying that I don't have any control over the native WebView. You can load web pages into a WebView that isn't onscreen and push the view or add it to the on-screen layout at a later time. My issue is that when my webpage's URL is loaded into a WebView, I can't tell when the WebView es onscreen.

I have a webpage that "plays a video" using sprite sheets. The page is mobile-optimized, so it can get loaded into Android and iOS WebViews. I'd like to know is when the page is visible so only after that I can play the video. I don't want users to catch the video mid-stream because the WebView lags in presenting itself.

I can see some developers might wait until the whole page has finished pulling in all the assets from the page before making it visible to the user. So, I don't want the "video" to start before that time. I can't rely on window.onload because that event fires even when the WebView isn't onscreen or visible.

How can I acplish this from the client side, with some JavaScript, preferably?

[Edit] To be clear, I'm implying that I don't have any control over the native WebView. You can load web pages into a WebView that isn't onscreen and push the view or add it to the on-screen layout at a later time. My issue is that when my webpage's URL is loaded into a WebView, I can't tell when the WebView es onscreen.

Share Improve this question edited Mar 1, 2013 at 17:13 user2122422 asked Mar 1, 2013 at 6:21 user2122422user2122422 1034 bronze badges 10
  • 2 Did you try jQuery $(document).ready(function() { // code for showing video }); – Stiger Commented Mar 1, 2013 at 6:52
  • 1 as a simple fix, I would just add a slight delay for the animation using setTimeout() – kennypu Commented Mar 1, 2013 at 6:52
  • 1 Hmm, I'm not using jQuery at all. Is it a good idea to include that library just for that ready function? What's the regular ol' JavaScript equivalent? – user2122422 Commented Mar 1, 2013 at 7:19
  • 1 @kennypu What if the WebView isn't displayed until user action? I wouldn't be able to predict that... – user2122422 Commented Mar 1, 2013 at 7:19
  • 1 i thought the webpage was loaded when the webview is displayed? if not, you can just do that; show/refresh the page when the webview is displayed. – kennypu Commented Mar 1, 2013 at 7:27
 |  Show 5 more ments

3 Answers 3

Reset to default 3 +25

Take a look at the Safari Web Content Guide. Scroll down to the Supported Events table. I am thinking (or hoping) that the pageshow event will do what you are hoping for. There is also the focus event.

Looks like using these events for mobile Safari would be as easy as

<body onpageshow="onPageShow();">

I am less familiar with Android, but I will look into it real quick.

EDIT: The onpageshow solution should work the same way in Android 2.2 and above as it does in iOS 4.0 and above. As for whether it works the way you need it to, I am not entirely sure. Let me know!

It is not possible to control the webview using JavaScript. If its not too late to change the design of the app, using native APIs will give you more control of the webview.

You could insert a timeout in the webpage before loading the video. It might be worth a shot.

you can use phonegap library:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    // Now safe to use the PhoneGap API
}

phonegap is very good for handle events and more action in webview.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744970595a4603941.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信