前端学习之路Electron——实现iframe

在你的入口文件下我的是main.js

    let BrowserView = electron.BrowserView
    let view = new BrowserView();
    mainWindow.setBrowserView(view);
    view.setBounds({x:0,y:120,width:1920,height:960})
    view.webContents.loadURL('https://www.baidu.com/');

main.js部分:

var electron = require('electron') 

var app = electron.app   

var BrowserWindow = electron.BrowserWindow;

var mainWindow = null ;
app.on('ready',()=>{
    mainWindow = new BrowserWindow({
        width:1920,
        height:960,
        webPreferences:{ nodeIntegration:true}
    })
    require('./main/menu')
    mainWindow.webContents.openDevTools()
    mainWindow.loadFile('demo.html')

    let BrowserView = electron.BrowserView
    let view = new BrowserView();
    mainWindow.setBrowserView(view);
    view.setBounds({x:0,y:120,width:1920,height:960})
    view.webContents.loadURL('https://www.baidu.com/');

    mainWindow.on('closed',()=>{
        mainWindow = null
    })

})

注意

这种方式会导致你内嵌的网页会受到下面这段代码的宽高影响

 mainWindow = new BrowserWindow({
        width:1920,
        height:960,
        webPreferences:{ nodeIntegration:true}
    })

window.open方法实现(子级窗口)

    <div id="myweb">
        open myweb
    </div>

html部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <button id="btn">
        打开新的窗口
    </button>
    <div>

    </div> -->
    <a id="aHref" href="https://www.baidu.com/">打开外部链接</a>
    <div id="myweb">
        open myweb
    </div>
    <script src="./render/openweb.js"></script>
</body>
</html>

JS:

const myweb = document.querySelector('#myweb');
myweb.onclick = function(e){
    window.open('https://www.baidu.com/')
}
const {shell} = require('electron');
const aHref = document.querySelector('#aHref');
aHref.onclick = function(e){
    e.preventDefault();
    let href = this.getAttribute('href');
    shell.openExternal(href)
}
const myweb = document.querySelector('#myweb');
myweb.onclick = function(e){
    window.open('https://www.baidu.com/')
}

你可能感兴趣的:(Electron)