Skip to content

Instantly share code, notes, and snippets.

@dannvix
Created June 24, 2015 15:44
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dannvix/dc0efdbb75bf79a79d1c to your computer and use it in GitHub Desktop.
Save dannvix/dc0efdbb75bf79a79d1c to your computer and use it in GitHub Desktop.
Create Electron's BrowserWindow with dynamic HTML content
// main.js for Electron
var app = require("app"),
BrowserWindow = require("browser-window");
app.on("window-all-closed", function() {
app.quit();
})
var mainWindow = null;
app.on("ready", function() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
center: true,
resizable: true,
frame: true,
transparent: false,
});
mainWindow.setMenu(null);
// create BrowserWindow with dynamic HTML content
var html = [
"<body>",
"<h1>It works</h1>",
"</body>",
].join("");
mainWindow.loadUrl("data:text/html;charset=utf-8," + encodeURI(html));
mainWindow.openDevTools();
mainWindow.on("closed", function() {
mainWindow = null;
});
});
@gersonfa
Copy link

gersonfa commented Aug 17, 2017

Didn't work for me, doesn't load anything. Is there any other way to load dynamic html in a new window?

@ajmakoni
Copy link

@gersonfa for the code above to work, change line 27:
mainWindow.loadUrl("data:text/html;charset=utf-8," + encodeURI(html));
It should be:
mainWindow.loadURL("data:text/html;charset=utf-8," + encodeURI(html));

@acherkashin
Copy link

@dannix, but how to force electron to load scripts? Let's say if your dynamic html contains <script src="./index.js"></script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment