1

I'm writing some HTML code with ElectronJS. I need to use JQuery within HTML, so I installed JQuery with npm install jquery.

But what file should I import to use JQuery?

<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="??"></script>
        <script>
            $(document).keydown(function(event) {
                alert(event.which)
            })
        </script>
    </head>
    <body>
        foobar
    </body>
</html>

In <script src="??"> what should I write?

(Sorry for poor English)

gxvr
  • 296
  • 2
  • 17
QueN
  • 163
  • 1
  • 1
  • 13
  • 1
    Possible duplicate of [Electron: jQuery is not defined](https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined) – Justinas Aug 21 '19 at 13:11

1 Answers1

1

Just put <script src="node_modules/jquery/dist/jquery.min.js"></script> in your html.

Note: In order for your scripts to be executed on electron, you need to write as follows inside your html:

  <script>
         if (typeof module === 'object') {
             window.module = module;
             module = undefined;
          }
   </script>

   <script src="node_modules/jquery/dist/jquery.min.js"></script>
    <script>
      $(document).keydown(function(event) {
                alert(event.which)
        })
   </script>
    //Put your other scripts here

     <script>
        if (window.module) module = window.module;

     </script>
gxvr
  • 296
  • 2
  • 17
  • 1
    I installed jQuery using npm install jquery --save. For some reason I needed to use the full Windows path. ex: /electron_apps/test_app/app1/node_modules/jquery/dist/jquery.min.js The relative path ie. node_modules/... did not work. – Paul Flahr Aug 23 '20 at 02:30
  • Try: Download jQuery manually then put in your project folder. Instead of using node modules path you can write the path where you store your jQuery file. Example if you stored it inside assets folder within your project write: @PaulFlahr – gxvr Aug 26 '20 at 14:00