IOS 微信 webview 中使用 alert 时不显示当前域名

Author Avatar
tanglijun 11月 28, 2018

在 IOS 微信中,系统 alert 会在对话框上自动添加网址,对于用户体验不是很友好。

问题描述

调用系统 alert 时,系统默认带上当前网址的域名,用户体验不好

before

解决方案

我们可以重写 alert 方法,利用 iframe 中子 windowalert 方法

window.alert = function(name){
  var iframe = document.createElement("iframe")

  iframe.style.display = 'none'
  iframe.setAttribute('src', 'data:text/plain')

  document.body.appendChild(iframe)

  frames[0].window.alert(name)

  iframe.parentNode.removeChild(iframe)
}

after

参考资料

许可协议:署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
本文链接:https://tanglj.cn/2018/11/28/do-not-show-host-in-ios-wx-webview-with-alert/