当前位置: 首页 > 工具软件 > elevator.js > 使用案例 >

Electron项目html页面加载node.js脚本

顾乐心
2023-12-01

最近在研究Electron开发桌面程序,发现html页面不能加载脚本,找了好多资料才找到问题的root cause。

1. 首先需要搞清楚,自己的脚本是用什么写的,node.js还是jquery。本文适用node.js写的脚本。

2. 需要在create BrowserWindow设置nodeIntegration=true,即整合node.js

    mainWindow=new BrowserWindow({ 
        width:368, 
        height:700,
        frame:false, //无边框
        resizable: false,
        webPreferences: {
            nodeIntegration: true
        }

    });

3. 如上index.html页面就可以正常调用index.js脚本了。

    //index.html

    <!DOCTYPE html>

    <html>

        <header>...</header>

        <body>...

        <script src="index.js" />

        </body>

    </html>

 类似资料: