首页 > 解决方案 > BeforeinstallPromt 事件仍然触发 添加到主屏幕 在 window.location 中设置任何值后提示不起作用

问题描述

在 window.location 中设置任何值后,谷歌的添加到主屏幕功能不起作用。

到目前为止做了什么?

参考:web-fundamentals-app-install-banners

在此实施期间,我正在捕获'beforeInstallPromptEvent'窗口并在以后需要时使用它。

PFB 相同的代码片段:

window.addEventListener('beforeinstallprompt', (e) => {
  deferredPrompt = e;
  // Update UI notify the user they can add to home screen
  showInstallPromotion();
});


btnAdd.addEventListener('click', (e) => {
  // hide our user interface that shows our A2HS button
  btnAdd.style.display = 'none';
  // Show the prompt
  deferredPrompt.prompt();
  // Wait for the user to respond to the prompt
  deferredPrompt.userChoice
    .then((choiceResult) => {
      if (choiceResult.outcome === 'accepted') {
        console.log('User accepted the A2HS prompt');
      } else {
        console.log('User dismissed the A2HS prompt');
      }
      deferredPrompt = null;
    });
});

上面的代码在正常的旅程中完美运行,但是一旦我在 window.location 中包含一些内容以转到安装在设备中的某个应用程序,它就会停止工作,

当 Truecaller 功能的以下代码与添加到主屏幕一起添加时,它会停止工作:

window.location='xxxxxsdk://some-url/';

我也尝试过使用其他选项重定向到类似location.assign()但仍然相同的问题的应用程序。

标签: javascriptgoogle-chromedom

解决方案


嗨)安装应用程序后尝试放置:

window.addEventListener('appinstalled', function() {
  // window.location = ...
});

这是文档:https ://developer.mozilla.org/en-US/docs/Web/API/Window/appinstalled_event


推荐阅读