0

I would like to integrate jQuery in my electron project, I need it because I need this code to run

$('#link').on('submit', function (e) {
  e.preventDefault();
  let $form = $(this),
    $select = $form.find('select');
  links = $select.val();
  if (links.length > 0) {
    for (i in links) {
      link = links[i];
      window.open(link);
    }
  }
});

I tried this code to fix it

  <script>if (typeof module === 'object') { window.module = module; module = undefined; }</script>
  <script src="scripts/jquery.min.js"></script>
  <script src="scripts/vendor.js"></script>
  <script>if (window.module) module = window.module;</script>

which is the one that everyone uses, but it doesn't work, I hope you can help me.

  • Does this answer your question? [Electron: jQuery is not defined](https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined) – tpikachu Jun 09 '20 at 08:14

2 Answers2

0

install it using NPM

npm i jquery

import it like this

var $ = require('jquery');

then have fun

edit: check this link $ is not defined - using jQuery with electron.js you will find a complete example in the answers there

0

The problem is caused by the fact that electron runs on node, hence module is defined and this part of the jQuery source, add jQuery to the module and not the window object:

if ( typeof module === "object" && typeof module.exports === "object" ) {
  // set jQuery in `module`
} else {
  // set jQuery in `window`
}

Try installing jQuery with npm and then simply add this script tag:

<script>window.$ = window.jQuery = require('jquery');</script>
Jan Bussieck
  • 1,039
  • 12
  • 26