Category » JavaScript

How to find the latest stable npm version and upgrade npm?

In order to find the npm latest stable version, you need to visit the npm documentation web page and check the footer.

Earlier it used to mention the version number in footer but now it provides a link to the GitHub hosted repository https://github.com/npm/cli/releases/latest; so, you can directly check go to this link and check the latest stable npm version available.

Continue reading »

Should I install npm package locally or globally?

If I am beginner with npm, then this question will definitely come to my mind. To get an answer to this, you should ask the following questions to yourself:

  • How will I use the installed npm package?
  • Will I use it as a dependency in my module or will I use it as a command line tool?

 

Install locally

 

If you are going to include the package (say “slick-carousel”) using statement like “require” in node.js and then use it in your module.

npm install <package_name>

Continue reading »

How to use cookies in JavaScript?

A Cookie is a piece of information that is stored locally on users browser and is sent with each HTTP request to the server. HTTP being a stateless protocol, cookies provide a mechanism by which we can remember user preferences and share information between web pages.

The cookies have a limit of 4KB thus when this limit is reached, you will likely be unable to create a new cookie. To make it more clear, size of all cookies under a specific domain cannot exceed 4KB.

Continue reading »

Understanding & Installing Node.js and NPM

We have been using client side/front end JavaScript to build dynamic pages where we embed the JavaScript code in HTML file, react to events and make AJAX calls for fetching data from the server.

Node.js allows you to write server side and networking applications with JavaScript.

Node.js is an open-source, cross-platform run-time environment that executes JavaScript code server-side. It is build on built on Chrome’s V8 JavaScript engine. It uses an asynchronous event-driven model and is designed for writing scalable internet applications, notably web servers.

Continue reading »

JSON – An Overview

JSON

JSON stands for JavaScript Object Notation. It is a format for storing and exchanging data. It is based on a subset of the JavaScript Programming Language.

The file type for JSON files is “.json” and MIME type for JSON text is “application/json“.

Here are the characteristics of JSON:

  1. It is a lightweight data-interchange format.
  2. It is easy for humans to understand.
  3. It uses Javascript format but it is a text which makes it is completly language independent as text can be read and parsed by any programmng language.

Continue reading »

How to limit the number of characters that can be entered in a textarea or input field?

Edit Text

In web pages which take input/feedback from the customers it’s a good idea to limit the type and size of data that can be entered so that spam can be avoided.

Let see how we can limit the number of characters that can be entered in a text box.

The simplest approach I follow is to use the jQuery “Limit” plugin. Click here to download the plugin.

Continue reading »

How to Add Smooth Page Scroll to Top using jQuery

Scroll to Top

You would have come across websites that provide an easy way to scroll to the top of the webpage as you move down the page, its really a good idea if the webpage is long and you want to provide a better user experience. In this article, we will show you how to add a smooth scroll to top effect using jQuery.

We will create a button on the footer of the page which when clicked will scroll the page to the top smoothly, we will use css to postion the button at a fixed position on the bottom right of the page and jQuery to provide the scrolling effect and make the button visible only if the page is scrolled down, instead of being always visible.

Step 1: Add button link
Place the following code just before the closing </body> HMTL tag

<a href="#" id="scrolltotop" title="Back to top">Back to top</a>

Continue reading »

Disabling the form submit on press of ENTER key

enter key

At times you would have seen that when you are filling a form on a website and you hit “ENTER” key the form gets submitted. As a developer you may have come across a situation where you would have wanted to prevent this action. The solution to this is to disable the “ENTER” key using javascript. Here’s a code snippet that disables “Enter” key in HTML textbox:

<script type="text/javascript">

// <![CDATA[
function disableEnterKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (elem.type=='text'))  {return false;}
}

document.onkeypress = disableEnterKey;
// ]]>

</script>

Continue reading »

Back to top