Integrate jQuery into a electron app

后端 未结 3 1421
臣服心动
臣服心动 2021-02-09 16:41

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

相关标签:
3条回答
  • 2021-02-09 17:26

    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".

    0 讨论(0)
  • 2021-02-09 17:33

    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>
    
    0 讨论(0)
  • 2021-02-09 17:36

    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

    0 讨论(0)
提交回复
热议问题