Download Blob as file in Javascript

While we are working with Javascript we may find our self in a situation where we need to let users download blob as File.

So In this post, I’ll share basic javascript function which allows us to download blob as a file in the browser.

const downloadBlobAsFile = function(data, filename){
				const contentType = 'application/octet-stream';
        if(!data) {
            console.error(' No data')

        if(!filename) filename = 'filetodonwload.txt'

        if(typeof data === "object"){
            data = JSON.stringify(data, undefined, 4)

        var blob = new Blob([data], {type: contentType}),
            e    = document.createEvent('MouseEvents'),
            a    = document.createElement('a') = filename
        a.href = window.URL.createObjectURL(blob)
        a.dataset.downloadurl =  [contentType,, a.href].join(':')
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    // call the function 
    const data = "some data";
    const fileName = "filetodonwload.txt";
    downloadBlobAsFile(data, fileName);

You can find a working sample here. 


Difference between == and === in JavaScript

So we know in javascript we have == and === comparisons. Why two? What are the differences between them?

So what is the difference between followings

'1' == 1

this returns true.

when it comes === equals

'1' === 1

this returns false.

So what it is?

The triple-equals comparator compares both type and contents

So ‘1’ === 1 contains string value and int value. Even contents are same they are in different types. So it is not equals.

Double equals tries to help with type coercion. But it’s not always the help we want. So keep in mind when you are using above equal comparisons.

let run long runing task in background in javascript

So When I am in javascript development I had a long running javascript method which delays page loading. So I wanted to make it run in background to protect responsiveness of application. So I found Differed execution in Jquery.  Using differed execution we can run functions in background and do another stuffs as we want. And we can pass callback function to execute on after finishing the long running function.

Code Sample on jsfiddle

Get query string values from Javascript

So now I am writing here utility function that i am using to get query string values.

Just Passe the query string name and function will return value.

function queryString(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) return "";
else { var x = results[1]; return x.replace(/%20/gi, ' '); }

Find Bootstrap screen size in Javascript

Some times we want to know bootstrap screen size from javascript. When I am developing I needed it and Now I am using following function. Calling it I can get bootstrap screen size like “xs”,”md”,”sm”,”lg”

function findBootstrapEnvironment() {
var envs = ['xs', 'sm', 'md', 'lg'];

$el = $('

for (var i = envs.length - 1; i >= 0; i--) {
var env = envs[i];

$el.addClass('hidden-' + env);
if ($':hidden')) {
return env

Bind javascript array or object to html drop down in Jquery

So when we are in developing we have to populate dropdown box with javascript objects. Some times we have to write more dirty codes to do that.

I am using following Jquery function to populate dropdown box with javascript array or object.

//element=> html select element id,
//source=> javascript array
//showDefault=> show default --None-- : optional
//defIndex=> default selected index : optional
function bindCombo(element, source, showDefault, defIndex) {
var k = (typeof element == "string") ? $("#" + element)[0] : element[0];
k.options.length = 0; var r = 0, j = source.length;
if (showDefault) { k.options[0] = new Option("--None--", "-1"); r = 1; }
if (j > 0) {
if (typeof source[0] == "object") { for (var i = 0; i < j; i++) { k.options[i + r] = new Option(source[i].Value, source[i].Id); }; }
else { for (var i = 0; i < j; i++) { k.options[i + r] = new Option(source[i], source[i]); }; }
if (defIndex) $(k).val(defIndex);

So if we want to bind following array to drop downbox simply we can add html attribute as below and call the above function

<select id=”test”></select> 

<b>var arr = ["one","two"];</b>


Now it will be populated.

More over we can bind list of following object

 function Pair(Id, Value) {
this.Id = Id;
this.Value = Value;
var arr=[];
arr.push(new Pair(1,first));
arr.push(new Pair(2,second));


Further we can provide whether to show default “–None–” or set selected index using ending two parameters in function. Just only pass true or false.

json date to javascript date

json date string comes like below.


While I was developing I wanted convert into javascript date and get date in “mm/dd/yyyy” format.

The I am using below function to get it done.

function parseJsonDate(jsonDateString) {

var date; var newDate;
if (jsonDateString) {
date = new Date(parseInt(jsonDateString.replace('/Date(', '')));
newDate = date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear();

 return newDate;

I think this might help you….