January 2016

HTML5 template tag hello world tutorial

HTML5 template tag can be used to define template in html and instantiate it when needed. The instantiated template can be inserted in any DOM read more

Javascript – how to view text inside DOM element

Javascript DOM method textContent can be used to get the text content inside a DOM. This can be useful to obtain content from a web read more

Javascript – DOM appendChild example

Javascript DOM method appendChild can be used to append a child to existing DOM element. For example it can be used to append an Image, read more

CORS – cross origin request tutorial and example in PHP

CORS (cross origin resource sharing) can be used to make AJAX requests to different origins. Here are some points to note: CORS un-aware browser will read more

Convert svg xml text to png image using canvas

HTML5 canvas can used to draw image and also be converted to base64 image data url using toDataURL. Here is an example which will convert read more

HTML5 canvas – draw image using svg xml string

HTML5 canvas can be used to draw image from svg xml string using drawImage(). For this the image source should be data:image/svg+xml;charset=utf-8,SVG_STRING. Here is code read more

HTML5 canvas – draw image using base 64 encoded png string

HTML5 canvas can be used to draw image from base64 encoded blob using drawImage(). In this case image source will be data:image/png;base64,BASE64_STRING. Here is code read more

HTML5 canvas draw image examples

HTML5 canvas draw image examples using drawImage(). Using image from html Using javascript Img object

PHP – call a function with arguments in array examples

PHP call_user_func_array() can be used to call a function with arguments in an array. This gives us the flexibility to pass variable number of arguments. read more

PHP – get function arguments examples

PHP func_get_args(), func_get_arg() and func_num_args() can be used to retrieve php function arguments inside a function. This may be useful in debugging. Here are code read more

HTML5 canvas clear rectangle example

HTML5 canvas clear rectangle example using clearRect(). The cleared rectangle becomes transparent and canvas background color is visible if set. Note that clearRect order is read more

HTML5 canvas rectangle example

HTML5 canvas rectangle example using rect() and fillRect(). This example uses canvas path, lineWidth, lineStyle, fillStyle.

HTML5 canvas line example

HTML5 canvas lineto example. This example uses canvas path, line width, line style, line cap.

HTML5 canvas basic example

HTML5 canvas can be used to draw graphic in web page HTML using Javascript. Here is a basic example of HTML canvas for beginners. HTML5 read more

Javascript DOMContentLoaded listener example

DOMContentLoaded event is fired when document DOM is ready and before images, etc. are loaded. It is similar to jQuery $(document).ready(). Here is code snippet read more

Javascript – ready vs on load event handler example

HTML document ready event is called before image assets, etc. are loaded on the page. window onload handler is called once all assets have been read more

PHP – get calling file name and line number using debug_backtrace

Sometime it is useful to log the calling filename and line number details of the calling file/function in php. One common use case if to read more

Javascript – img onload examples

Sometime we need to call a function after an image has been loaded. We can attach onload even handler on image either in html itself read more

Multiple onload handlers using vanilla Javascipt

Attaching a function to windows on load event can be done in multiple ways using plain vanilla Javascript. One option is to use wondow.onload where read more

Javascript – use querySelector to set dom element html

Javascript querySelector can be used to select and set html of a dom element using css selectors. This can be done with jQuery also. But read more

Python value in array (list) check

In Python we frequently need to check if a value is in an array (list) or not. Python x in list can be used for read more

Python string split examples

Python has in-built string split function which can be used to split a string on any delimiter and return a python list (array). If no read more

Python string quick start tutorial

Python built-in data type string is very frequently used in any python program. Here are some examples of using strings to get started with. Print read more

Linux – find and delete files older than 30 days

Often we need to delete old file on Linux or Unix which are old (say 30 days or more). Here are some commands which can read more

Gmail – how to find large size emails

Gmail can get full very soon with heavy file attachments being very common these days. In case your need to clean your gmail mailbox, easiest read more

PHP check if key exists in array

Checking if a key exists in an array and possibly has a non empty value are frequently used when writing php code. Accessing value of read more

PHP check if string contains something

Checking if a string contains a substring is very frequently occuring use case in PHP code. PHP functions strpos or stripos (case insensitive version) can read more

robots.txt disallow all example

Sometime we need to block all robots from crawling a web site. This can be needed if you have a stage or sandbox website for read more

PHP – check if a value is in array

Checking if a value exists in array is frequently used in php. PHP in_array() can be used for this check. By default it does not read more

How to use wildcard in robots.txt

By default Disallow entry in robots.txt is based on url prefix. To block every url beginning with /foo/ the following robots.txt can be used. User-agent: read more

WordPress mysql query to get all posts with a missing custom field

Handy mysql query to get all posts with a missing custom field. We’ll query post_type=page for the purpose of this tutorial. You will have to read more

PHP – split string examples

Splitting a string/text at a delimiter is very common use case in php. One can either use php explode (split on a fixed delimiter) or read more

Chrome – how to find a plugin directory location on Mac

It may be useful to look at Chrome plugin code at times for debugging purpose. Chrome plugins are stored in the following directory (for user read more

PHP regex – word boundary examples

Php regex can use word boundary character (\b) to match a word boundary. Beginning of string if following by a word character will also match read more

Perl command line – replace multi line comments

Perl command line one liner can be used to replace text in a file using regular expressions (regex). It is very powerful approach for automating read more

AngularJS hello world example

Angularjs is a web application framework which has gained lot of popularity in mobile and single page applications. It uses directive ng-app to represent root read more

Css :last-of-type selector – last child element of type

Css :last-of-type pseudo class apply to an element if it is last sibling among its parent’s direct child elements of same type. Here is an read more

Css :last-child selector – last child element

Css :last-child pseudo class apply to an element if it is last sibling in all direct child elements of its parent. It can be used read more

Docker quick start guide on Mac

Docker is an open-source project that automates the deployment of applications inside software containers. Here is Docker quick start guide on Mac. Install docker toolbox read more

Css :first-of-type selector – first child element of type

Css :first-of-type pseudo class apply to an element if it is first sibling among its parent’s direct child elements of same type. Here is an read more

Css :first-child selector – first child element

Css :first-child pseudo class apply to an element if it is first sibling in all direct child elements of its parent. Here are some examples. read more

Css :nth-child vs :nth-of-type

Css :nth-child and :nth-of-type pseudo classes look very similar initially. But these are slightly different. Few points to explain the difference: :nth-child considers all sibling read more

Python filter vs ifilter

Python filter is very handy python built-in function to filter iterables (e.g. lists) in python. But it returns a list which may consume huge memory read more

Jenkins – how to setup build cron

Jenkins supports setting build triggers using Jenkins crons. This way build will run at regular interval and possibly Jenkins can send email on failure. Here read more

Mysql how to dump schema of all databases

It is a good idea to frequently take schema dump of all your databases. This may be useful in case there is some accidental change. read more

Python filter list/iterable examples

Python filter is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that filter applies read more

Jenkins execute shell – how to continue on error

In case can you are using Jenkins shell commands to run tests and are running multiple commands, by default Jenkins exits the comment one shell read more

Selenium Phantomjs – check browser errors using pytest

Checking browser errors (including Javascript errors) is an important use case in testing a website. Javascript errors are hard to QA and can cause bad read more

Javascript – iterate over function arguments

Sometimes it is useful to iterate over function arguments programmatically. Function arguments can be access using arguments variable. Here is code snippet for this. Note read more

Jenkins – how to setup build failure email

Getting email of a build failure is a common use case in Jenkins. We would want to get notified whenever any build fails and when read more

PHP unserialize online

This online php unserialize tool can be used to apply php unserialize function to given string.

PHP md5 online

This online php md5 tool can be used to apply php md5 function to given string.

PHP base64_decode online

This online php base64 decoder can be used to apply php base64_decode function to given string.

PHP base64_encode online

This online base64 encoder can be used to apply php base64_encode function to given string.

PHP – Regex OR (alternation) examples using pipe

Regex OR (alternation) in php can be matched using pipe (|) match character. This is very frequently used in regular expressions. Following are some ways read more

Python selenium – print browser log (including javascript errors) with phantomjs

It is useful to find out javascript errors in a page automatically. This can help in site quality test automation. Here is selenium code snippet read more

Bash – how to compare file timestamps

Sometimes we need to perform some action if a file is newer than other file. This can be used to recreate generated file if source read more

CSS direct child selector quiz

Question: In following html code, select which text will have green background?

Online Quizes

PHP – How to log custom data in apache access log

Apache lets you log a specific apache sub process environment variable in access log. PHP lets you set apache sub process env variable using apache_setenv. read more

Linux – find files containing specific text

Handy command on Linux/Mac to find all files matching a pattern containing a specific text. This will also print file name. $ find . -type read more

Jenkins how to clone a project

Sometimes we need to create a replica of a project or project with similar configuration in Jenkins. This helps in avoiding entering project configuration data read more

PHP – preg_replace back reference (in replacement text) examples

Sometimes we need to use back reference in PHP preg_replace to put back the matched pattern. One example can be if you want to append read more

Css source map example using gulp-sourcemaps

If you are concatenating multiple css files into once css file, it is useful to have sourcemap in your combined css file in development environment. read more

Gulp quick start tutorial

Gulp is a toolkit to automate tasks primarily in web development workflow. It can automate things like css/js minification, concatenation, etc. Here is quick tutorial read more

LXC (Linux Containers) – quick start tutorial on Ubuntu

LXC (Linux Containers) is an operating-system-level virtualization environment for running multiple isolated Linux systems (containers) on a single Linux control host. Here is a quick read more

jQuery – find total number of DOM elements

To get total DOM elements including children use the following code (this includes root html element also) $(‘*’).length To get all DOM elements in body read more

PHP – how to use inline image for better site performance

For small images it is a good idea to embed inline images using img src value data:image/png;base64. This will prevent one extra round trip to read more

How to remove jquery-migrate.js from wordpress

In case you want to remove jquery-migrate.js for performance reasons, here is the code you can use. Note that this may break some plugins or read more

Csslint – quick command-line tutorial and how to use it for pre-deployment sanity check

Csslint can be used to check the syntax of a css file. This can be used in release/deployment scripts to check the syntax of css read more

WordPress – how to exclude specific tag posts from a tag archive page

WordPress will display all posts on a tag archive page which are tagged with that tag. Sometimes we want to exclude posts of more specific read more

Svn – how to make file executable on Linux

To make a file executable in svn on Linux, it property svn:executable has to be set as ON. $ svn propset svn:executable ON sync.py $ read more

CSS – :nth child, :nth-last-child, :first-child, :last-child pseudo classes examples

CSS nth-child, nth-last-child, first-child, last-child pseudo class selectors (:nth-child(), :nth-last-child(), :first-child, :last-child) are pretty powerful css selectors to create very useful styles in html. Here read more

How to disable spellcheck in textarea

It is a good idea to disable spell check in textarea element if it is not meant for pure english text. To disable spell check read more

CSS – child vs descendant selector examples

CSS direct child can be specified using A > B and any descendant can be specified using A B. This should be used with care read more

Vim – how to change current character case to uppercase or lowercase

It is often required to toggle the case of current character in Vim. Vim has lot of other powerful commands related to changing case of read more

PHP – how to catch errors using set_error_handler

PHP set_error_handler can catch various type of php errors in a specific portion of the code. To restore the previous error hander one can call read more

awk sum examples

Here are some awk examples to sum an integer column from a file or stdin (standard input) stream. Data used in these examples Awk sum read more

awk – if condition examples

Awk is pretty powerful command line utility to filter lines from a stream. Here are some awk if condition examples. Data used in these examples: read more

Dig – list all dns records

To list all DNS records of a domain name use the following dig command (after replacing microsoft.com with desired domain) $ dig microsoft.com any ;; read more

Svn – how to diff last committed change

To see the diff of last committed change follow these steps: Find last revision id $ svn update $ svn log -l 1 r4546 | read more

Svn – short diff without context

Using svn command line option Use the following diff command to avoid diff context $ svn diff –diff-cmd=diff -x -U0 Option -x passes option -U0 read more

Can ssh keys generated as one user be used as a different user

Ssh keys are generated in pair and are valid as a pair. These are not tied to any user. Sometimes user info goes into the read more

Interview Questions

Bash – add a number to a variable

To add a number to a variable in bash, there are many approaches. Some of these are: Declare variable as integer Once a variable is read more

Vim – highlight and move to matching bracket

To find and move to matching bracket (curly, square or round) use the following command inside vim once you are in one of the brackets. read more

How to override priority and change frequency in Yoast xml sitemap

It is important to maintain healthy xml sitemap for your site. In case you are using Yoast SEO plugin to generate xml sitemap for a read more

bash – extract urls from xml sitemap

Command to get urls from a sitemap url: To get these in a bash variable for iteration, use sub-shell command execution as shown below: Use read more

WordPress – customize posts per page for tag, category and date archive pages

The following code can be used to have separate value of posts per page for various archive pages (category, tag, date, author, etc.)

Php regex delimiter examples

PHP requires regex patterns to have valid delimiters around it. Some languages may not require delimiters (e.g. python). To make code more readable, sometimes we read more

Svn – how to edit log message for a committed change

Command to edit a committed revision Example command for revision r4448 $ svn propedit svn:log –revprop -r r4448 hook setup for allowing editing log message read more

CSS lint (syntax check) online

CSS lint check – check if syntax of given css code is valid and see errors online. Sample css code Valid css .foo { width:200px; read more

XML lint (validate syntax) online

XML lint check – validate if syntax of given xml data is correct and see errors online. Sample valid xml Simple xml <tag1> foo </tag1>

WordPress – customize category and tag links

In case you need to create a custom link for category or tag, the you can term_link filter. This can be useful in case you read more

Svn log of all files in a directory

Sometime we need to see svn log of all files in a directory (including all files in all its subdirectories). Here are some approaches. Using read more

PHP – convert array to associative array

Convert using array_flip() This works if we need index as value. Using foreach iterator In case we need to set associative array value using array read more

PHP – how to initialize static variables

Sometimes we need to populate static variables in php (e.g. populating associative arrays from existing arrays). Easiest way is to define a static function (e.g. read more

Bash associative array tutorial

Bash associative arrays are supported in bash version 4. Here is a quick start tutorial for using bash associative arrays. Declare and initialize associative array read more

WordPress – an approach to bulk add tags to posts

We’ll bulk add a tag to all posts in a category as a simple use case. But this approach can be modified to handle other read more

WordPress – query to find term_taxonomy_id from category name

Mysql query – Find term_taxonomy_id from category name Use the following query to find term_taxonomy_id for a category name after replacing “Tutorial” with the category read more