页面加载时如何使navigator.vibrate在Onload上工作 [英] how to make navigator.vibrate to work on Onload when page loads

查看:223
本文介绍了页面加载时如何使navigator.vibrate在Onload上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让手机/设备在页面加载时振动.现在,只有当我们单击按钮进行振动时,振动才起作用.

I want to make the phone/device to vibrate when the page loads. Right now the vibrate is working only when we click the button to vibrate.,

但是onload标记不起作用.给我一个解决方案.

But the onload tag it is not working. Give me a solution to fix this.

我也通过添加window.onload = example1();感到很累.但仍然无法正常工作.

I have even tired by adding window.onload = example1(); too. But still not working.

<html>
    <head>
        <script type="text/javascript">
           function example1() {
             // Vibrate for 500ms
             navigator.vibrate([500]);
           }
        </script>     
    </head>
    <body onload="example1()">
        <div><button onclick="example1()">Vibrate</button></div>
    </body>
 </html>

页面加载后,手机/设备应根据我们设置的时间间隔振动.

When the page loads, phone/device should vibrate based on the time interval we set it.

推荐答案

您不能,

在Chrome中,此方法需要用户手势.

这里摘录了臭虫 实施了此措施.

Here is an excerpt of the motivations from the crbug which implemented this measure.

振动API被社交工程的网站和广告滥用,我们希望通过用户激活(也就是用户手势或用户发起的操作)来控制振动,以更好地保护用户.

The vibrate API is abused by sites and ads for social engineering, and we'd like to gate vibrate with user activation (aka. user gesture or user initiated actions) to better protect user.

因此无法进行加载.

在Firefox中,他们确实选择了按域请求用户确认,这意味着对于该浏览器,您可以在第二次加载时触发此方法,但是用户必须单击接受".在上次访问的提示消息中.

In Firefox, they did choose to ask for user's confirmation on a per domain basis, this means that for this browser, you could, at second load trigger this method, but the user would have had to click "accept" on a prompt message from a previous visit.

这是按照 W3C建议由于一些令人费解的原因而更加关注隐私.

This is by the way in accordance with the W3C recommendations which is more concerned about privacy for some convoluted reasons.

对于其他浏览器,我不确定它们是否支持此方法.

For other browsers, I'm not sure they even support this method.

这篇关于页面加载时如何使navigator.vibrate在Onload上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆