在 Edge 浏览器中,window.showModalDialog 方法已被废弃,无法直接使用。如果要在 Edge 下使用类似的功能,可以考虑使用 window.open 方法来替代。\n\n下面是一个示例代码,演示了如何通过 window.open 方法实现类似于 window.showModalDialog 的效果:\n\njavascript\nfunction openModal(url, width, height) {\n // 计算窗口位置\n var left = (window.screen.width / 2) - (width / 2);\n var top = (window.screen.height / 2) - (height / 2);\n\n // 打开一个新窗口\n var newWindow = window.open(url, '_blank', 'modal=yes,toolbar=no,location=no,status=no,menubar=no,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);\n\n // 等待窗口关闭\n newWindow.onbeforeunload = function() {\n // 在这里可以处理窗口关闭后的逻辑\n console.log('Modal window closed');\n };\n}\n\n// 示例用法\nvar modalUrl = 'http://example.com/modal.html';\nvar modalWidth = 400;\nvar modalHeight = 300;\n\nopenModal(modalUrl, modalWidth, modalHeight);\n\n\n在上述示例中,openModal 函数接收三个参数:url 表示要打开的模态窗口的 URL,width 和 height 表示窗口的宽度和高度。函数内部使用 window.open 方法打开一个新窗口,并设置了一些属性来实现模态窗口的效果。同时,可以通过 newWindow 对象的 onbeforeunload 事件来处理窗口关闭后的逻辑。\n\n请注意,由于 Edge 浏览器的安全策略,可能会阻止弹出窗口,因此需要确保在浏览器设置中允许弹出窗口或禁用相关的安全设置。此外,还需要注意在模态窗口中执行的代码可能会受到浏览器的安全限制。

Edge 浏览器替代 window.showModalDialog 方法 - 使用 window.open 实现模态窗口

原文地址: https://www.cveoy.top/t/topic/pyYn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录