admin管理员组文章数量:1406924
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
3 Answers
Reset to default 3 +25Take 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.
本文标签: androidHow to find out if WebView is displayedonscreenvisible using JavaScriptStack Overflow
版权声明:本文标题:android - How to find out if WebView is displayedonscreenvisible using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744970595a2635209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论