一、核心模块
shell
const { shell } = require('electron') var aHref = document.querySelector('#ahref') aHref.onclick = function (e) { e.preventDefault() let href = this.getAttribute('href') shell.openExternal(href) }
二、全部代码
index.js
var electron = require('electron') //引入electron组件 var app = electron.app //引入组件app var BrowserWindow = electron.BrowserWindow //窗口引用 var mainWindow = null //申明打开窗口 app.on('ready', () => { //app初始化参数 mainWindow = new BrowserWindow({ windth: 800, height: 800, webPreferences: { nodeIntegration: true, //使用node功能 contextIsolation: false, enableRemoteModule: true } }) mainWindow.webContents.openDevTools() //默认打开调试模式 mainWindow.loadFile('index.html') //打开窗口加载的页面 mainWindow.on('close', () => { //窗口关闭时,释放页面 mainWindow = null }) })
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!-- <a href="https://www.baidu.com">百度</a> --> <a id="ahref" href="https://www.baidu.com">百度</a> <script src="./render/index.js"></script> </body> </html>
/render/index.js
const { shell } = require('electron') var aHref = document.querySelector('#ahref') aHref.onclick = function (e) { e.preventDefault() let href = this.getAttribute('href') shell.openExternal(href) }
三、效果
electron .