Created
June 24, 2015 15:44
-
-
Save dannvix/dc0efdbb75bf79a79d1c to your computer and use it in GitHub Desktop.
Create Electron's BrowserWindow with dynamic HTML content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 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));
@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
Didn't work for me, doesn't load anything. Is there any other way to load dynamic html in a new window?