javascript click event onload page href whatsapp

问题描述 投票:0回答:5

我在我的页面中有锚标记。我想触发点击事件onload。这意味着我想打开这个链接"whatsapp://send?text=test&phone=+123456789"去whatsapp。如果手动单击,但链接页面上没有工作,则链接正在运行。与jquery相同的结果。反正有没有这样做?

这就是我所拥有的:

<body onload="document.getElementById('openwhatsapp').click();">
    <a id="openwhatsapp" href="whatsapp://send?text=test&phone=+123456789">whatsapp</a>
</body>

为了澄清这一点,与http://的联系没有问题,它的工作原理。只是不为whatsapp://工作?

javascript jquery mobile whatsapp
5个回答
1
投票

首先,不要使用内联JS。这是不对的。

现在来解决方案。你为什么不试试像

document.body.onload = function(){
     window.location.href = "whatsapp://send?text=test&phone=+123456789";
}

如果您仍想坚持使用内联JS,我建议使用javascript:为您的代码添加前缀,因为有些浏览器会忽略内联JS。

所以现在你的代码就变成了

<body onload="javascript:document.getElementById('openwhatsapp').click();">
    <a id="openwhatsapp" href="whatsapp://send?text=test&phone=+123456789">whatsapp</a>
</body>

0
投票

也许location.href对你来说是一个很好的解决方案。

<body onload="location.href = 'whatsapp://send?text=test&phone=+60123456789">
     <a id="openwhatsapp" href="whatsapp://send?text=test&phone=+60123456789">whatsapp</a>
</body>

0
投票

尝试这样的事情

<href="intent://send/0123456789#Intent;scheme=smsto;package=com.whatsapp;action=android.intent.action.SENDTO;end">

当你点击链接时发生了什么?任何错误或空白页?


0
投票

你可以使用,$(window).load(function() {})甚至$(document).ready(function() {})

$(window).load(function() {
  location.href = 'whatsapp://send?text=test&phone=+60123456789'
});

$(window).load(function() {
  location.href = 'whatsapp://send?text=test&phone=+60123456789'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a id="openwhatsapp" href="whatsapp://send?text=test&phone=+60123456789">whatsapp</a>

运行此代码段,它正在运行


0
投票

如果你想在加载时运行它,那你为什么不使用:

window.location = "whatsapp://send?text=test&phone=+123456789"
© www.soinside.com 2019 - 2024. All rights reserved.