Skip to content

jQuery issue on render side #345

Closed
Closed

Description

@deecoolist

everytime loading jquery from render side (any.html) via dom <script src="jquery.js"></script> resulting "Uncaught ReferenceError: jQuery is not defined" or "Uncaught Error: ... requires jQuery";

but if I load jQuery via require:

<script>
var $ = jQuery = require('jquery'); // as node_modules
/*
 * or localy load.
 * var $ = jQuery = require('./jquery');
 */
</script>

(on every page) It was fine!, but module.exports get polluted by jQuery's methods

so every page either local or external site whom use jQuery would fail to properly load since jQuery header:

(function( global, factory ) {

    if ( typeof module === "object" && typeof module.exports === "object" ) {
        // For CommonJS and CommonJS-like environments where a proper window is present,
        // execute the factory and get jQuery
        // For environments that do not inherently posses a window with a document
        // (such as Node.js), expose a jQuery-making factory as module.exports
        // This accentuates the need for the creation of a real window
        // e.g. var jQuery = require("jquery")(window);
        // See ticket #14549 for more info
        module.exports = global.document ?
            factory( global, true ) :
            function( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            };
    } else {
        factory( global );
    }

// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {

are they any solution for this kind of matter?
Thanks.

Activity

  1. jsonnull commented on May 22, 2014

    @jsonnull

    I just cooked up a solution to this, as I was having the same issue.

    Simply include this script immediately after your <script src="jquery.js"></script>:

    <script>
        try {
            $ = jQuery = module.exports;
            // If you want module.exports to be empty, uncomment:
            // module.exports = {};
        } catch(e) {}
    </script>
  2. deecoolist commented on May 22, 2014

    @deecoolist
    Author

    Thanks @jsonnull , i have try that on local file surely did work, but when you loadUrl from external site e.g. http://getbootstrap.com/index.html it seem wont do the trick. 👍 I think the bug at libchromiumcontent. I have try some others tricks like inject the script from main.js;

    right after instantiate BrowserWindow;

      mainWindow.webContents.on('did-start-loading', function() {
        mainWindow.webContents.executeJavaScript("var $ = jQuery = require('jquery'), mainWindow = require('remote').getCurrentWindow();");
      });

    first load ok, but next load are mess.

  3. jsonnull commented on May 22, 2014

    @jsonnull

    @deecoolist , The problem you have is that jQuery is attaching itself as a global variable on the window object of your mainWindow.... however, the main script you are using with mainWindow has no notion of the "window" object, as there could be multiple windows to which you may refer. In this case, your window object is some descendant of your mainWindow variable.

    For this to work, you're going to have to reorganize your code so that the jQuery variable is a global not attached to the window, or use some kind of injection or message-passing as you have done here.

  4. deecoolist commented on May 22, 2014

    @deecoolist
    Author

    @jsonnull thanks I got your ideas.

  5. ranmocy commented on Jun 2, 2014

    @ranmocy

    As #254 is a better solution:

    window.$ = window.jQuery = require("libs/jquery-1.10.2.js");
  6. jsonnull commented on Jun 2, 2014

    @jsonnull

    @ronmocy Only if you're not going to serve your HTML outside of atom-shell.

  7. ranmocy commented on Jun 2, 2014

    @ranmocy

    @jsonnull You mean reuse the code right? How about include the jQuery again in the normal way?

  8. gliurn commented on Feb 27, 2015

    @gliurn

    @deecoolist have you found a solution to make JQuery a global variable? it would help me for my app, i got the same issue with JQuery and i need it to load a website on a webview

  9. DogsForArms commented on Mar 17, 2015

    @DogsForArms

    Thanks very much for this quick solution.

  10. added a commit that references this issue on May 25, 2015
  11. SantoshSrinivas79 commented on Jun 24, 2015

    @SantoshSrinivas79

    @deecoolist what was the solution to this? i need the same functionality like you've described

  12. rigelk commented on Aug 19, 2015

    @rigelk

    Just for reference @fountainhead , it did the trick for me :

    <script type="text/javascript" src="js/jquery.min.js" onload="window.$ = window.jQuery = module.exports;"></script>
  13. eric-naguras commented on Aug 21, 2015

    @eric-naguras

    Thanks rigelk.
    This worked for me.
    My app works on OSX but would give jquery module not found errors on Windows.
    Your solution solved that.

  14. raghav-axero commented on Aug 27, 2015

    @raghav-axero

    Hi,

    I added the following code

    onload="window.$ = window.jQuery = module.exports

    on external page:

    It did add the jQuery object but with the value of module.exports.

    What about the core jQuery file code?

    I can't access any functions which jQuery exposes after that like jQuery.fn etc

    Can anyone suggest any solution for adding jQuery.js on an external site loaded in Electron via:

    mainWindow.loadUrl('http://mysite.com/external.html');

  15. added a commit that references this issue on Oct 5, 2015
  16. thonatos commented on Oct 18, 2015

    @thonatos

    I use this in script :

    var $ = require(path.join(process.cwd(), 'frontend/assets/vendors/jquery/dist/jquery'));

  17. mendo9 commented on May 18, 2016

    @mendo9

    I found this worked to load jquery in window of an external website.

    main.js

    mainWindow = new BrowserWindow({
        webPreferences: {
          nodeIntegration: true,
          webSecurity: true,
          preload: path.resolve(path.join(__dirname, 'path/to/preload.js'))
        },
        width: 900,
        height: 900
      });
    
    mainWindow.loadURL("https://externalsite.com");

    preload.js

    document.addEventListener("DOMNodeInserted", function(event) {
        if (!!window && !(!!window.$)) {
            window.$ = window.jQuery = require('path/to/jquery.js');
        }
    });
  18. Bronts commented on Oct 28, 2016

    @Bronts

    @rigelk I use your solution but I modified like this
    update:

    <script type="text/javascript" src="js/jquery.min.js" onload="window.$ = window.jQuery;"></script>
    

    with module.export it will notice module is not defined if set

    webPreferences: {
                nodeIntegration: false, 
            }
    

    Thanks for your solution.

    This works fine for loading remote url with jQurey.

  19. AizazAhmad commented on Feb 16, 2018

    @AizazAhmad

    Thanks @jsonnull. i was stuck to load custom jquery. it takes mine 2 days. after that i got ur solution: $ = jQuery = module.exports;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions