Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a modal window and load HTML from render process?

this is my current project structure:

main.js
index.html
download.html
src/index.js
src/style.css

main.js will create a BrowserWindow and load index.html, which loads index.js and style.css.

I'd like to create a new modal window and load download.html when a button is clicked in index.html.

here is codes from my index.js:

btn.onclick = function() {
  let win = new remote.BrowserWindow({
    parent: remote.getCurrentWindow(),
    modal: true
  })
  win.loadURL(`file://${__dirname}/download.html`)
}

but it's loading file://download.html, isn't __dirname existed in render process?

like image 800
wong2 Avatar asked Mar 20 '17 10:03

wong2


People also ask

How do you trigger a modal on page load?

Answer: Use the Bootstrap . modal('show') method modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.

What is a modal window HTML?

A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal.

How do you create an electron window?

Windows can be created from the renderer in two ways: clicking on links or submitting forms adorned with target=_blank. JavaScript calling window. open()


2 Answers

Just tested this code (below) and it works. Directory structure is:

main.js
app (directory)
--- index.html
--- app.js (where the code below exists)
--- modal.html

How are you accessing remote?


"use strict";

const { remote } = require('electron');


function openModal() {
  let win = new remote.BrowserWindow({
    parent: remote.getCurrentWindow(),
    modal: true
  })

  var theUrl = 'file://' + __dirname + '/modal.html'
  console.log('url', theUrl);

  win.loadURL(theUrl);
}
like image 146
spring Avatar answered Sep 22 '22 03:09

spring


Using __dirname will give the directory of the current file. In the main process it will be according to the filesystem. In a renderer process it will be in the context of the browser and be based on the accessed uri.

You likely want to use app.getAppPath or app.getPath(name) instead.

like image 24
Steve Buzonas Avatar answered Sep 20 '22 03:09

Steve Buzonas