你好,
我在will-navigate事件(https://github.com/atom/electron/blob/master/docs/api/web-view-tag.md)文档中查看了以下句子。
调用 event.preventDefault() 没有任何效果。
那么我们如何防止 webview 导航到其他页面呢? 我打算在用户单击 webview 上的 URL 链接时启动默认浏览器。
这是我关于渲染器进程的代码。
webview.addEventListener('will-navigate', function(e) {
e.preventDefault();
require('electron').shell.openExternal(e.url);
});
使用此代码,当我单击 webview 上的 URL 链接时,webview 将导航到新页面,并且默认浏览器也打开显示新页面。 我希望 webview 停止导航到新页面。
#1378 的副本。
我没有看到发布的解决方案?
这对我有用,可以防止通过用户单击链接进行导航并通知应用程序有关请求的 url。 使用预加载并注入:
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
setTimeout(function () {
var path = e.target.href;
ipcRenderer.sendToHost('element-clicked', path);
}, 100);
return false;
}, true);
});
请注意document.addEventListener('click', fn, true)
的第三个参数
参考: https ://stackoverflow.com/questions/6157486/jquery-trap-all-click-events-before-they-happen/6157660#6157660
最有用的评论
这对我有用,可以防止通过用户单击链接进行导航并通知应用程序有关请求的 url。 使用预加载并注入:
请注意
document.addEventListener('click', fn, true)
的第三个参数参考: https ://stackoverflow.com/questions/6157486/jquery-trap-all-click-events-before-they-happen/6157660#6157660