0

I have a strange situation where jQuery is not loading into the window object. My app is an Electron app. My startup page does in fact load jQuery. But while the startup page is loading, I have some code that decides whether to reload the browser window with a different url. That url is another html page in the app and it contains a reference to the jQuery library in the script tag. jQuery does appear to load in this new page because I am able to open DevTools and type $ in the console and it indicates that it is loaded. However, in my javascript code, which is embedded in the page, the $ is not recognized. I am not loading any other scripts that could conflict with the jQuery script. The jQuery script is the only script loaded. What could be preventing jQuery from being attached to the window object? This only happens when I load that new page using window.location.href. If I don't load that page, but simply use jQuery in the startup page, jQuery is attached to the window object.

Here is how I am loading jQuery:

<head>
    <script type="text/javascript" src="../../jquery/jquery-3.2.1.min.js"></script>
</head>

I have tried waiting for jQuery to load using:

function defer(method) {
    if (window.jQuery) {
        method();
    } else {
        setTimeout(function() { defer(method) }, 50);
    }
}

defer(function () {
    alert("jQuery is now loaded");
});

but window.jQuery never gets set.

20
  • There are not many details we can build answers on top of. The code above perfectly works. Commented Jan 3, 2018 at 9:41
  • Maybe this is an issue with Electron?? Commented Jan 3, 2018 at 9:42
  • 1
    @AndroidDev I was asking Vinod about "chrome's inbuilt jquery", because I doubt it too. If your second page is in a sub folder, you have to add ../ in the script relative path. Commented Jan 3, 2018 at 9:52
  • 1
    As far as I know, the Chromium that electron uses does have a $ in window. I don't think it is jQuery though, just an in-built thing.. Commented Jan 3, 2018 at 9:52
  • 1
    Possible duplicate of Electron: jQuery is not defined Commented Jan 3, 2018 at 9:59

1 Answer 1

2

In Electron, jquery isn't loaded as a script.

Use this instead (in your header):

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

and install jQuery as a package with npm install --save jquery

If that doesn't work, try using

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

//put all of scripts here
<script src="path/to/jquery"></script>

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

Or you can use:

<script>window.$ = window.jQuery = require('./path/to/jquery');</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Actually it is. I have no problem loading jQuery or any script in Electron using the script tag.
Sorry, I don't understand. Have you tried this? I'm not sure, but this is what I use in my projects
The second solution works. I'm not sure why this is necessary since my startup page just loads jQuery the normal way with just a script tag. This appears to be an issue with Electron. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.