Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate jQuery into a electron app

I'm trying to add jquery functionality to a desktop app written in electron Using the electron-quick-start repo i'm adding the downloaded jquery file to the main.html file like so:

<script> require("./jquery.min.js"); </script>

or so:

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

Then in the index.js file i'm adding code in the createWindow function, since that seems the proper place, but to be honest any place i try gets me the same error more or less.

mainWindow.$ is undefined and the same goes for BrowserWindow and app

mainWindow is defined inside the createWindow function like so: mainWindow = new BrowserWindow({width: 800, height: 600})

and BrowserWindow is declared on top of the file like so: const BrowserWindow = electron.BrowserWindow

Any idea where i'm going wrong, what declarations i should change/add?

Thanks in advance

like image 238
omu_negru Avatar asked Jun 10 '16 09:06

omu_negru


People also ask

How do I add jQuery to my electron application?

The most straightforward method to add jQuery to your Electron application is to perform the following steps: Create a suitable dedicated folder within your Electron project, such as /src/contents/jquery/ (assuming your main index.html page is in the /src/ folder).

How do I add bootstrap to my electron project?

Create a suitable dedicated folder within your Electron project, such as /src/contents/bootstrap/ (assuming your main index.html page is in the /src/ folder). That folder will contain Bootstrap CSS and JS files.

What is electron JS compiling?

Electron's docs says, " Electron JS is a runtime framework that allows the user to create desktop-suite applications with HTML5, CSS, and JavaScript". Here, you might be asking what is compiling our JS code we write.

How do I install electron JS on Ubuntu?

Electron JS is a dev dependency, and it should be installed globally. Install it with the help of npm install -g electron --only=dev. Once installed, the package.json should have Electron as a devDependency. (If not please add it manually.)


Video Answer


3 Answers

While using electron, some additional symbols are also inserted into DOM causing problems. So, you can use jquery as follow

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js" onload="window.$ = window.jQuery = module.exports;"></script>

Notice the code inside "onload".

like image 69
Hari Das Avatar answered Oct 16 '22 07:10

Hari Das


When you call require inside index.js, or the main process, it's looking for the node module. So you'll need to install jQuery via npm and save it as a dependency in your apps package.json file.

npm install jquery --save

Then your index.js should theoretically see it just fine using

let $ = require('jquery');
mainWindow.$ = $;

Refer to the Node.JS section for installing jQuery. This is what Electron uses.

--

OLD ANSWER

Inside your main.html, just include the JavaScript like you would any traditional JS file.

<script src="./jquery.min.js"></script>
like image 24
Shakespeare Avatar answered Oct 16 '22 07:10

Shakespeare


To integrate jQuery into your Electron Application follow these simple steps

Step 1: Run in terminal

npm install jquery --save

Step 2: Add this line to your angular.json or angular-cli.json file

 "build": {
        "options": {
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.js", //add this line
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
...
...
     }
  }

Step 3: Finally add this line to your index.html file

 <!-- Need this for jQuery electron -->
  <!-- Insert this line above script imports  -->
  <script>if (typeof module === 'object') {
    window.module = module;
    module = undefined;
  }</script>

  <!-- Insert this line after script imports -->
  <script>if (window.module) module = window.module;</script>
</head>

You can also use this template

like image 31
Mostasim Billah Avatar answered Oct 16 '22 06:10

Mostasim Billah