Examples overview
In this section, we have collected a set of guides for common features that you may want to implement in your Electron application. Each guide contains a practical example in a minimal, self-contained example app. The easiest way to run these examples is by downloading Electron Fiddle.
Once Fiddle is installed, you can press on the "Open in Fiddle" button that you will find below code samples like the following one:
- index.html
- main.js
- preload.js
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Hello World!</title>
<metahttp-equiv="Content-Security-Policy"content="script-src 'self''unsafe-inline';"/>
</head>
<body>
<h1>Hello World!</h1>
<p>
We are using Node.js <spanid="node-version"></span>,
Chromium <spanid="chrome-version"></span>,
and Electron <spanid="electron-version"></span>.
</p>
</body>
</html>
const{ app,BrowserWindow}=require('electron')
const path =require('path')
functioncreateWindow(){
const win =newBrowserWindow({
width:800,
height:600,
webPreferences:{
preload: path.join(__dirname,'preload.js')
}
})
win.loadFile('index.html')
}
app.whenReady().then(()=>{
createWindow()
app.on('activate',()=>{
if(BrowserWindow.getAllWindows().length===0){
createWindow()
}
})
})
app.on('window-all-closed',()=>{
if(process.platform!=='darwin'){
app.quit()
}
})
window.addEventListener('DOMContentLoaded',()=>{
constreplaceText=(selector, text)=>{
const element =document.getElementById(selector)
if(element) element.innerText= text
}
for(const type of['chrome','node','electron']){
replaceText(`${type}-version`, process.versions[type])
}
})
How to...?
You can find the full list of "How to?" in the sidebar. If there is something that you would like to do that is not documented, please join our Discord server and let us know!
© GitHub Inc.
Licensed under the MIT license.
https://www.electronjs.org/docs/latest/tutorial/examples