Category Archives: JavaScript
Dirty checking to warn for unsaved changes using jQuery
Copy following code snippet into the application. dirtyCount() method return the number of fields have unsaved changes. var dirtyChecking = function () { $(‘input, select, textarea’).each(function () { var ele = $(this); ele.attr(‘data-old’, ele.val()); // Look for changes in the value ele.on(“change keyup paste click”, function (event) { if (ele.attr(‘data-old’) != ele.val()) { ele.addClass(‘unsaved’); }…
Drag and drop from scratch using JavaScript
An example code to implement drag-ability in to an HTML element from scratch without using JavaScript library. JavaScript functions to provide drag-ability: function DDElement(ele, event) { addEvent(ele, “mousedown”, downHandler, true); addEvent(document, “mouseup”, upHandler, true); if (event.stopPropagation) event.stopPropagation(); else event.cancelBubble = true; if (event.preventDefault) event.preventDefault(); else event.returnValue = false; function upHandler(e) { e = e ||…
Javascript not working – NetworkError: 404 Not Found
Yesterday, I have deployed an application on Ubuntu server but the javascript was no longer working after configuring the virtual host. I have taken following steps to find the problem: Viewed the HTML source code using firebug and found page not found error Checked the .htaccess file, but there were no problem Tried a test…
Cross browser event listener
Sometimes, we need to attach or detach an action with an element for specific event where listener function depends on browser. By using following code snippet anyone can do that in all browsers: function addEvent(obj, evType, fn, useCapture) { useCapture = (useCapture === undefined ? false : useCapture); if (obj.addEventListener) { obj.addEventListener(evType, fn, useCapture); }…
Form validation using jQuery
I have written a simple form validation script using jQuery for one of my projects though it have improvement opportunity. Anyone can use it easily by following four simple steps below: STEP 1: Add jQuery plugin to your application STEP 2: Copy following codes in to a JavaScript file and add the file after jQuery…
Find mouse pointer position using JavaScript
Sometimes we need to know the mouse cursor position to perform a certain action at that place. For example, I need to show up a popup div near the mouse cursor position when user click over a particular field/link. You can get the mouse cursor position using following codes: function getCursorPosition(e) { e = e…
JavaScript coding standards/conventions
The long-term value of software is depended of the quality of codebase and it helps to reducing the weakness of programs. That’s why we should follow standards while writing Js code which will increase the code quality. I am trying here to give you some idea/suggestion to write better Js code. Below are some suggestions…
Custom prompt window using JavaScript
Below is a simple JavaScript code to create a custom prompt pop-up to get user input and set it to target field. /** * Open a custom prompt window to get user input * @param l String, Input label * @param v String, Default value of input box * @param len Integer, Max length of…
Implementation of fly-out flash message in your application
Now a days usability is one of the major issues for web application. Many web applications with good functionality did not get enough popularity because lack of usability. Flash message is one of the most important things of any application which may improve usability of an application. Here, I have explain steps for implementing fly-out…
Disable right click to prevent page content
Some web developers and client want to disable the right click to prevent the theft their web content specially image. It can do by using a simple line of html code below though it can bypass in many ways. <body oncontextmenu=”return false;”> Update: I have got a best reference below: No Right Click Script