Site icon Ryadel

Electron - How to add jQuery and Bootstrap to your project

We already talked about Electron a number of times, as it's one of the most interesting development frameworks available nowadays. For those who don't know it yet, we'll briefly recap what it is: Electron – formerly known as Atom Shell – is an open-source framework designed to build desktop GUI applications using HTML, CSS and JavaScript. To put it in other words, it allows web developers to use their existing knowledge to create native desktop clients for the most used operating systems – including Windows, Linux, MacOS and so on. The project is developed and maintained by GitHub and runs using Node.jsruntime for the back-end part and Chromium for the front-end.

For additional info regarding the Electron framework and a simple guide to setup a "Hello World!" project tutorial with Visual Studio 2017, we strongly suggest to take a look at this post. In the following article, assuming you already went through that and therefore you already have an Electron project , we'll briefly explain how to add JQuery and Bootstrap to that project.

Adding jQuery

Let's start with jQuery, the "write less, do more" Javascript library created by John Resig: you most likely know it, since it's one of the most popular client-side libraries since years. In the unlikely scenario you don't, we strongly suggest to take a look at the jQuery official website and to the jQuery Wikipedia entry.

At the time of writing the latest general available version of jQuery is 3.3.1: we're going to add this version to our Electron application.

Method #1: JavaScript Resource

The most straightforward method to add jQuery to your Electron application is to perform the following steps:

That's about it!

If your Electron application has multiple html pages, you just have to repeat the process for any other html file where you want to use jQuery.

Method #2: NPM Package

If you don't want to meddle with JavaScript files manually you can take advange of the Node.js module system and include jQuery that way.  To do that, perform the following steps:

That's it.

Again, in case your Electron application has multiple html pages, you need to add the above line within any other jQuery-enabled html file.

Adding Bootstrap

Let's now see how to add Bootstrap, also known as the most popular HTML, CSS, and JS library in the world - as said by the Bootstrap official website. In the unlikely scenario you don't know it, we'll briefly introduce it as well. Bootstrap is an open source toolkit for HTML, CSS, and JS development; its most notable features include: Sass variables and mixins, responsive grid system, extensive prebuilt components, and powerful plugins built on jQuery. If you need to know more, visit its official website and the Bootstrap Wikipedia entry.

At the time of writing the latest general available version of Bootstrap is 4.1.3: we're going to add this version to our Electron application.

Method #1: CSS & JavaScript Resources

IMPORTANT: as you can see, jQuery is also present as one of the Bootstrap optional packages. If you plan to use both Bootstrap and jQuery in the same page/project, be sure to only include it once: both methods shown earlier will work, as the include order above (suggested by the official Bootstrap setup guidelines) is preserved.

That's about it!

If your Electron application has multiple html pages, you just have to repeat the process for any other html file where you want to use jQuery.

Method #2: NPM Packages

If you prefer to include the NPM packages instead of adding the JS files manually, you can use the same method that we explained earlier for jQuery.

Here's what you need to add to your package.json file:

And these are the lines of code to add to the index.html page's <head> section:

It's worth noting that the CSS file cannot be included that way: it must be added using the Method #1.

This post is part of a series of articles, tutorials and guides on the Electron development framework. To read the other posts, click here!

 

*/ ?>
Exit mobile version