Ande Caleb
Ande Caleb

Reputation: 1204

window is undefined, Document is undefined - Electron

i am working on an application and trying to use mostly jquery, i have tried all the examples online, but i cant seem to get where my error is. please can someone review my code and tell me what i am doing wrong.. heres my code..

const {app, document, BrowserWindow, Menu} = require('electron')
const path = require('path')
const url = require('url')

//define jquery..
window.$ = require('jquery')(window);

  // Keep a global reference of the window object, if you don't, the window will
  // be closed automatically when the JavaScript object is garbage collected.
  let win

  function createWindow () {
    // Create the browser window.
    win = new BrowserWindow({
      name:"AtikuDesk",
      width: 396, 
      height: 356, 
      frame:false,     
      transparent: true,
      toolbar: false
    });

    // and load the index.html of the app.
    win.loadFile('assets/index.html')

    // Open the DevTools.
    win.webContents.openDevTools()

    // Emitted when the window is closed.
    win.on('closed', () => {
      // Dereference the window object, usually you would store windows
      // in an array if your app supports multi windows, this is the time
      // when you should delete the corresponding element.
      win = null
    })
  }

  // This method will be called when Electron has finished
  // initialization and is ready to create browser windows.
  // Some APIs can only be used after this event occurs.
  app.on('ready', createWindow)

  // Quit when all windows are closed.
  app.on('window-all-closed', () => {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })

  app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (win === null) {
      createWindow()
    }
  })

  menuarr = [
    {
      label:'Home',
      submenu:[
        {label:'About App'},
        {label:'view statistics'},
        {label:'get help'}
      ]
    },
    {
      label:'Exit',
      click(){
         app.quit()
      }

    }

  ];

  var menu = Menu.buildFromTemplate(menuarr);
  Menu.setApplicationMenu(menu);


function clsApp(e){
   app.quit();
}
$('#clsbtn').bind('click', clsApp);

The last function doesnt work, it doesn't work, it displays an error that window is undefined, and when i try using this code either.. without Jquery,

document.querySelector('#clsbtn').addEventListener('click', clsApp)

it keeps telling me that document is undefined. i havent been able to find a suitable solution online, would be glad if someone helps me fix this.

i prefer handling all my events and actions from seperate javascript files and not in the inline script tags on the html pages.

thanks.

Upvotes: 0

Views: 1930

Answers (1)

JeffProd
JeffProd

Reputation: 3148

Remove this code from the main JS file. Then in index.html :

<script>window.$ = window.jQuery = require('jquery');</script>
<script src="index.js"></script>

And index.js :

function clsApp(e){
   app.quit();
}
$('#clsbtn').bind('click', clsApp);

Upvotes: 1

Related Questions