How to Build Simple Terminal like Website using jQuery ?
Last Updated :
09 Nov, 2021
Terminal is one of the most common tool used by developers. Many applications, frameworks, and even programming languages have more function which can be invoked using command line interface. Most of the computers come with at least one terminal in their operating system with command prompt now most replaced by cross-platform powershell in windows, and Linux console in Linux based OS's.
By now most of you have understood terminal in as system application but how can we build terminal like website in a browser. For that JavaScript has got our back, these terminal likes and feels like a system terminal but are not as powerful as them, but they do the work for us, and thanks to some developers we got some libraries to help us out rather than writing from scratch. Some libraries are jQuery.terminal, Xtermjs, for this tutorial we will use JQuery.terminal
Now open your favorite code editor and create our base html file mostly index.html and call our dependencies
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<!--content type : HTML -->
<meta http-equiv="content-type"
content="text/html; charset=UTF-8">
<!-- Making viewport responsive -->
<meta name="viewport" content=
"width=device-width,minimum-scale=1,
initial-scale=1">
<!-- Loading jQuery, jQuery.terminal,
and style sheet -->
<script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script src=
"https://unpkg.com/jquery.terminal/js/jquery.terminal.min.js">
</script>
<link rel="stylesheet" href=
"https://unpkg.com/jquery.terminal/css/jquery.terminal.min.css" />
</head>
<body>
</body>
</html>
The body of web page is empty because it is the place where we will write our JavaScript code. Let us write our first command
JavaScript
// In body tag
$('body').terminal({
iam: function (name) {
this.echo('Hello, ' + name +
'. Welcome to GeeksForGeeks');
}
}, {
greetings: 'GeeksForGeeks - A place to'
+ ' learn DS, Algo and Computer'
+ ' Science for free'
});
In above JavaScript code, we are using jQuery to get body part of the document into terminal function. Then we create another function inside the terminal function which takes an argument. iam is the command we created which prints your name passed as argument and welcome to GeeksForGeeks.
Greetings in a default command which prints at the top of page for every time the page loads. It also plugin also contains an error command which prints out when the command is not present
Simple iam command
We can also write additional command like founder command which prints the founder name of GeeksForGeeks and help command which prints. Both of these commands are passed without arguments
JavaScript
$('body').terminal({
iam: function (name) {
this.echo('Hello, ' + name
+ '. Welcome to GeeksForGeeks');
},
founder: function () {
this.echo('Sandeep Jain');
},
help: function () {
this.echo('iam - iam command and'
+ ' pass your name as argument'
+ '\nfounder to know the founder');
},
}, {
greetings: 'GeeksForGeeks - A place to'
+ ' learn DS, Algo and Computer Science'
+ ' for free'
});
available commands
Now, we will change some styling using the style tag, we use green color for all text and increase the text size
HTML
<style type="text/css">
.terminal,span,.cmd,div {
--color: rgba(0, 128, 0, 0.99);
}
.terminal, span {
--size: 1.4;
}
</style>
The complete code is below
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type"
content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,
minimum-scale=1,initial-scale=1">
<script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script src=
"https://unpkg.com/jquery.terminal/js/jquery.terminal.min.js">
</script>
<link rel="stylesheet" href=
"https://unpkg.com/jquery.terminal/css/jquery.terminal.min.css" />
<style type="text/css">
.terminal,
span,
.cmd,
div {
--color: rgba(0, 128, 0, 0.99);
}
.terminal,
span {
--size: 1.4;
}
</style>
</head>
<body>
<script>
$('body').terminal({
iam: function (name) {
this.echo('Hello, ' + name +
'. Welcome to GeeksForGeeks');
},
founder: function () {
this.echo('Sandeep Jain');
},
help: function () {
this.echo('iam - iam command and '
+ 'pass your name as argument\n'
+ 'founder to know the founder');
},
}, {
greetings: 'GeeksForGeeks - A place to'
+ ' learn DS, Algo and Computer '
+ 'Science for free'
});
</script>
</body>
</html>
Complete output with devtools
You have build a simple interactive terminal website and customized it. jquery.terminal can also do some other things like
- Formatting and Syntax Highlighting.
- JSON-RPC where everything is on the server (found in Interpreter section on the wiki).
- Changing prompt.
- Masking passwords.
- Authentication.
- Combining commands with Pipe operator like in UNIX terminal.
- Keyboard shortcuts (list of build in you can find here).
- Reading text from users.
- Handling Emoji (if system don’t do that out of the box like Windows10).
- Executing commands from JavaScript.
- Invoking commands and terminal methods from Server.
- Updating lines.
- Saving state in URL hash and execute saved commands.
You can learn more about the jQuery.terminal from docs and github
Similar Reads
How to design window manager using jQuery Simone Plugin ? In this article, we will learn to design window manager using Simone plugin which is completely based on HTML, JavaScript, and jQuery. It gives single web page applications with simple desktop-like features.Download the pre-compiled files from the official website before the following code implement
6 min read
How to make a basic dialog using jQuery UI ? In this article, we will be creating a Basic dialog using jQuery UI. Approach: First, add jQuery UI scripts needed for your project. <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js
1 min read
How to design desktop functionality using jQuery-lwd plugin ? This article will help you to design a basic desktop engine using the jQuery-UI lwd plugin. This plugin is very light weighted and totally based on JavaScript and CSS.The desktop engine provides some following features for web developers -MovableResizableFocus on active windowRestore windows on clic
2 min read
How to design complex layout for webpage using jQuery EasyUI ? EasyUI is a HTML5 framework for using user interface components based on jQuery, React, Angular and Vue technologies. It helps in building features for interactive web and mobile applications saving a lot of time for developers.In this article, we will learn to design the basic and complex layout fo
4 min read
How to use a jQuery library in your project ? In this article, we will see how to use the jQuery library in a project. There are two methods to add a jQuery library to a project which are Include jQuery library from CDN linkDownload the jQuery library from the official website Include jQuery library from CDN link: CDN stands for Content Deliv
2 min read
How to Use the Magnific Popup jQuery Plugin? Magnific Popup is a fast, light, mobile-friendly and responsive lightbox and modal dialog jQuery plugin. It can be used to open inline HTML, ajax loaded content, image, form, iframe (YouTube video, Vimeo, Google Maps), and photo gallery. It has added animation effects using CSS3 transitions. Approac
2 min read