AngularJS directives and custom directives

Angular JS directives and custom directives

Angular is very popular JavaScript framework and custom directives are the heart of the angular. We will take look at the directives.

Angular directive

Directives are the command, tells angular what operations can angular do on the DOM element, attribute or the children of the element. These angular directives extend the HTML attributes. Angular directives are prefixes by ng-. Following are the some popular directives by angular.

ng-app: It initializes the angular application.

ng-init: It initializes the data of application.

ng-model: It provides the access to the model data for binding to the HTML controls.

For example

<div ng-app="firstApp" ng-init="firstName='John'">
<p>This is angular js application</p>
Change the name: <input type="text" ng-model="firstName" />
<p>My name is {{firstName}}

In above example firstApp is the name of the angular application. The name is not mandatory. You can keep blank as well. Next attribute is ng-init which initializes the data used by the application. By using the ng-model textbox can change the value of the firstName variable defined in the model. And finally we have used {{firstName}} to display the actual value of the variable.

Continue reading

Form input type range element (Slider control)

Input Type Range

HTML5 gives us very useful slider control. Lets look at the syntax and how to use the slider control.

<input type="range" name="price" min="1" max="10" value="1" />

Here are the different attributes you can use with range input.

  • min: specify the minimum value allowed.
  • max: specify the maximum value allowed.
  • step: specify the value of the interval.
  • value: specify the default value of the slider.

You can use event onchange to track selected value.

<input type="range" max="10" min="1" name="quantity" value="1" onchange="alert('Selected value: '+ this.value);" />


Select the quantity of the product:

Check the browser compatibility for the input type color

HTML5 web storage – offline storage solution for the web

Using HTML5 you can store data into user’s browser. Before HTML5, there was only one way to store data using cookies. This web storage stores data in key/value pair. You can use this technique to store data offline.

How to check that browser supports web storage?

if(typeof(Storage) !== “undefined”)
// Browser supports web storage. So you can use local and session storage
// Browser does not support web storage.

There are two types of the web storage

  1. Local Storage
  2. Session Storage

Local Storage

You can store data into local storage like

localStorage.key = “value”;

And you can retrieve it as

var localStorageValue = localStorage.key;

This storage is persisted even you closes and reopen the browser.

Session Storage

This storage is same as the local storage. One thing differs from local storage is that, the data will be lost if user closes the browser. So if you want to store data per session, go with this storage.

You can store data as follows

sessionStorage.key = “value”;

After you can retrieve as

var sessionStorageValue = sessionStorage.key;

How to check/debug web storage is working or not?

Developer tools of the browser will show data stored into the web storage. Following image taken from describes the where you can find the web storage.

HTML5 web storage


You can check your browser supports how many HTML5 features just opening into your browser.

Enrich user interface of your web application with Bootstrap UI framework

Bootstrap is very reach UI framework, flexible and responsive. It is made by Twitter and hosted on github.

You can find the more details at

Let’s start to integrate this framework into your web application.

Very first you need to download framework files from bootstrap website mentioned above. Download will contain ZIP file (bootstrap-x.x.x-dist.ZIP)

After extracting the downloaded ZIP file, you will get three folders:

  • css: This folder contains style sheets files for the bootstrap framework and theme
  • fonts: This folder contains necessary files for the fonts
  • js: This folder contains JavaScript files for bootstrap framework.

For the further process I have used Visual Studio to create and manage web application. You can use any editor instead of the Visual Studio.

In the Visual Studio go to the menu:  File -> New -> Project. It opens New Project dialog. Select ASP.NET Web Application and give the name for the solution.

Enrich user interface of your web application with Bootstrap UI frameworkClick on OK then Select Empty project type.

select empty project template

Click OK to create project.

You will see solution created as below.


Right click on ‘WebAppWithBootstrap’ the solution select Add then select HTML Page.

Solution2Give the name as index.html


Index.html file is created opened in editor.

Now add folder ‘Bootstrap’ by right-click on solution and choose Add and then New Folder.


Then create two folder called ‘css’ and ‘js’ inside the folder ‘Bootstrap’


Now copy the ‘bootstrap.min.css’ file from css file folder of the extracted Bootstrap zip file to the css folder of the solution. Also copy the ‘bootstrap.min.js’ JavaScript file from js folder of the extracted Bootstrap zip file to the js folder of the solution.


Now we need to give references of the added css and js file into index.html file as follows.


Here we have added meta tag with viewport and set content width=device-width. This made your page responsive.

We have done with the integration of Bootstrap framework with web application.

Now open the page into browser.


jQuery Mobile Introduction

Touch-Optimized Web Framework for Smartphones & Tablets

Recently I worked on mobile application based on HTML5 and JQuery. I have used jQuery mobile framework. It is very simple to learn and use, if you already know jQuery. You can find out more stuff from below site.

You can view documentation from below site. This site optimized to open in all mobile browsers.

You can download necessary files to use jQuery mobile from below site.

jQuery mobile also provide CDN-hosted versions. These are minified and compressed with host images.

You need to copy and paste following lines into your project to use jQuery mobile framework.

<link rel="stylesheet" href="" />
<script src=""></script>
<script src=""></script>

jQuery mobile supports theme based UI. This framework also provides default themes. You can create your own custom theme by theme roller.

Metro style context menu using JavaScript and CSS

What is context menu?

When you right click on browser window, browser will show you a menu containing some common functions.
See following image for reference.

Metro style context menu using JavaScript and CSS

If you want to build your own custom context menu, follow the easy steps below.

Step 1 – HTML

<div id="context-menu">
<li><a href="#">menu 1</a> </li>
<li><a href="#">menu 2</a> </li>
<li class="list-devider">
<hr />
<li><a href="#">menu 3</a> </li>
<li><a href="#">menu 4</a> </li>
<li><a href="#">menu 5</a> </li>
<li class="list-devider">
<hr />
<li><a href="#">menu 6</a> </li>
<li><a href="#">menu 7</a> </li>

Step 2 – CSS

Following is CSS declaration for above context menu structure. This will give metro style look to context menu.

z-index: 1000;
position: absolute;
border: solid 2px black;
background-color: white;
padding: 5px 0;
display: none;
} #
context-menu ol
padding: 0;
margin: 0;
list-style-type: none;
min-width: 130px;
width: auto;
max-width: 200px;
#context-menu ol li
margin: 0;
display: block;
list-style: none;
zoom: 0;
padding: 5px 5px;
#context-menu ol li:hover
background-color: #efefef;
#context-menu ol li:active
color: White;
background-color: #000;
#context-menu ol .list-devider
padding: 0px;
margin: 0px;
#context-menu ol .list-devider:hover
/*background-color: #efefef;*/
#context-menu ol .list-devider hr
margin: 2px 0px;
#context-menu ol li a
color: Black;
text-decoration: none;
display: block;
padding: 0px 5px;
#context-menu ol li a:active
color: White;

Step 3 – JavaScript

To enable context menu, write down following script.

$(document).ready(function () {
try {
$(document).bind("contextmenu", function (e) {
$("#context-menu").css({ top: e.pageY + "px", left: e.pageX + "px" }).show(100);
catch (err) {

Note: Above script require JQuery to be included in document.

In above script we have set context menu function to our custom function. This function will prevent default context menu of browser and
will show custom context menu.

Here we can add following function when user clicks on document, will hide context menu.

$(document).bind("click", function (event) {

look at the beautiful metro style context menu tested in IE9.

Metro style context menu using JavaScript and CSS

Enjoy JavaScript…

Convert Text to Image using canvas

With using canvas we can export image with dynamic text. Let see example below.

HTML code

<div id="contentToWrite1" style="border: solid 1px gray; padding: 5px;">
first line
<br />
<br />
<div id="contentToWrite2" style="border: solid 1px gray; padding: 5px;">
second line
<canvas id="canvas" width="200" height="200" style="border: solid 1px gray;">
<br />
<h2>Exported Image</h2>
<img id="exportedImage" width="200" height="200" src="#" />
<br />
<input type="button" name="name" value="export canvas to image" onclick="exportImage();" />

JavaScript code

window.onload = function () {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.font = "20px Georgia";
var ele = document.getElementById("contentToWrite1");
var ele1 = document.getElementById("contentToWrite2");
if (ele.textContent) {
// for firefox
context.fillText(ele.textContent, 10, 90);
context.fillText(ele1.textContent, 10, 110);
} else {
// for other browser
context.fillText(ele.innerText, 10, 90);
context.fillText(ele1.innerText, 10, 110);
function exportImage() {
var img = document.getElementById("exportedImage");
img.src = canvas.toDataURL('image/png');

On windows load function creates object of canvas and context of canvas. We will use different properties and method of context to
render content on canvas.

  • context.font : This property used to set font style to content on canvas.
  • context.fillText(textToWrite,x,y): this method used to render text on canvas.

By using above methods we have rendered content on canvas, looks like below.

Convert Text to Image using canvas

When we click on button export ‘canvas to image’ image will be set to html image element below to canvas.

  • canvas.toDataURL: This is canvas method to render it as image.

Enjoy JavaScript…

Call function of the parent window from iframe window from JavaScript

You can call code of the parent window form child window.

f you open window by using iframe, you can communicate from child window with the parent.

Let’s see an example

Page1.html (Parent window)
        function HelloFromChild(name) {
            alert("Hello " + name + " , (called from child window)");
<iframe src="Page2.html" width="200"></iframe>
Page2.html (child window opened in iframe)
        function onClick() {
Iframe page
<input type="text" name="name" value="techie" id="name" />
<button onclick="onClick();">Call Parent Window Function</button>

When you open Page1.html in browser you will see the output as below. Click on the button to call parent function.

When you click on the button, onClick event will be fired. onClick event will call function from parent window by using parent variable. By using parent variable you can call function from parent window.

Call function of the parent window from iframe window from JavaScript


Check internet connection using JavaScript

Sometimes you need to check device have internet connection or not.

This can be done by simple JavaScript function

var connectionMessage = "Your device have internet connection.";
var noConnectionMessage = "Ohh! It seems that your device dont have internet connection.";
window.onload = checkInternetConnection;
function checkInternetConnection() {
  var isOnLine = navigator.onLine;
  if (isOnLine) {
  } else {

Enjoy JavaScript…

Making Field Read only in SharePoint 2010

Many times you need to disable the fields in SharePoint form for ‘New Item’. By using JavaScript we can disable field in easier way. Lets
discuss an example.

Using JavaScript

Find field id by inspecting element in browser’s developer tools. The filed id is something like this

Then write down few steps to disable it.

function DisableField()
    var element = document.getElementById('ctl00_m_g_efe2ea91_242b_4746_bb44_5a67e8cbc5c2_ff331_ctl00_ctl00_DateTimeField_')

Now you need to call the function ‘DisableField()’ on body load.

SharePoint gives out of the box functionality for JavaScript developer to inject custom function into function list of SharePoint function lists.

You can push ‘DisableField()’ function as follows.


Enjoy JavaScript…