diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 44b9bb4afae..e0d198ef383 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1159,6 +1159,14 @@ win.loadURL('http://localhost:8000/post', { }) ``` +#### `win.loadFile(filePath)` + +* `filePath` String + +Same as `webContents.loadFile`, `filePath` should be a path to an HTML +file relative to the root of your application. See the `webContents` docs +for more information. + #### `win.reload()` Same as `webContents.reload`. diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index d15325d5079..d269a27fd2d 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -625,6 +625,28 @@ const options = {extraHeaders: 'pragma: no-cache\n'} webContents.loadURL('https://github.com', options) ``` +#### `contents.loadFile(filePath)` + +* `filePath` String + +Loads the given file in the window, `filePath` should be a path to +an HTML file relative to the root of your application. For instance +an app structure like this: + +``` +| root +| - package.json +| - src +| - main.js +| - index.html +``` + +Would require code like this + +```js +win.loadFile('src/index.html') +``` + #### `contents.downloadURL(url)` * `url` String diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index b977e8766f9..22565dcf987 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -169,6 +169,9 @@ Object.assign(BrowserWindow.prototype, { getURL (...args) { return this.webContents.getURL() }, + loadFile (filePath) { + return this.webContents.loadFile(filePath) + }, reload (...args) { return this.webContents.reload(...args) }, diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 71e57ebee90..20025652ef8 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -2,6 +2,8 @@ const {EventEmitter} = require('events') const electron = require('electron') +const path = require('path') +const url = require('url') const {app, ipcMain, session, NavigationController} = electron // session is not used here, the purpose is to make sure session is initalized @@ -243,6 +245,17 @@ WebContents.prototype.getZoomLevel = function (callback) { }) } +WebContents.prototype.loadFile = function (filePath) { + if (typeof filePath !== 'string') { + throw new Error('Must pass filePath as a string') + } + return this.loadURL(url.format({ + protocol: 'file', + slashes: true, + pathname: path.resolve(app.getAppPath(), filePath) + })) +} + WebContents.prototype.getZoomFactor = function (callback) { if (typeof callback !== 'function') { throw new Error('Must pass function as an argument')