Set Focus to End of Text Input using Javascript and jQuery

If the text input already has some text and you wish to set the cursor to the end of the text input then here’s how it can be done. Using JavaScript In javascript simply add a onfocus event handler in the text input. <input type=”text” onfocus=”this.value = this.value;” value=”Some text”/> Using jQuery Define a function […]

Continue reading


How to get Base64 image from an URL or Image tag

You might need to convert a png/jpeg image to a base64 encoding. The following function does that for you. function getBase64FromImageUrl(url) { var img = new Image(); img.setAttribute(‘crossOrigin’, ‘anonymous’); img.onload = function () { var canvas = document.createElement(“canvas”); canvas.width =this.width; canvas.height =this.height; var ctx = canvas.getContext(“2d”); ctx.drawImage(this, 0, 0); var dataURL = canvas.toDataURL(“image/png”); var url= […]

Continue reading


Exact Time Measurement for Performance Testing

This post shows how to exactly measure performance for some code to execute. This is quite useful while doing performance testing. Here’s how it can be done. C-Sharp Stopwatch sw = new Stopwatch(); sw.Start(); // … sw.Stop(); Console.WriteLine(“Elapsed={0}”,sw.Elapsed); Javascript var t0 = performance.now(); doSomething(); var t1 = performance.now(); console.log(“Call to doSomething took ” + (t1 […]

Continue reading


How to Copy to Clipboard in Javascript

This post shows how to copy to clipboard in javascript. Manual Method Automatic copying to clipboard may be dangerous, therefore most browsers (except IE) make it very difficult. You could use the following function to for copy to clipboard functionality. function copyToClipboard(text) { window.prompt(“Copy to clipboard: Ctrl+C, Enter”, text); } The user is presented with […]

Continue reading


How to Fix No Transport Error with jQuery Ajax call in IE

If you are using cross domain Ajax requests in javascript then it may not run properly on IE 7, 8 or 9. For eg. let the ajax call be similar to this. $.ajax({ type: “POST”, url: ‘https://api.foursquare.com/v2/venues/search’, contentType: “application/json”, data: JSON.stringify(json), crossDomain: true, success: function (respData, status, xhr) { // success }, error: function (xhr, […]

Continue reading


Send a Message to Pebble Watch using Android App

You can send a message from your Android app to a connected Pebble watch. We will use PebbleKit Android, the library that enables communication with Pebble. To use it, add PebbleKit to your list of dependencies in the dependencies block (add this if it does not already exist): dependencies { // … compile ‘com.getpebble:pebblekit:3.0.0’ } […]

Continue reading


PHP Function that Returns the Date for a Specific Day Between Two Given Dates

Here’s a PHP function that returns the date for a specific day between two given dates. The function takes two dates formatted as YYYY-MM-DD and creates an inclusive array of the dates between the from and to dates. function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber) { $startDate = strtotime($startDate); $endDate = strtotime($endDate); $dateArr = array(); do { if(date(“w”, […]

Continue reading