Get Query Parameters by Name in JavaScript

Here’s a utility function to get URL’s query parameters by name. function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, ‘\\$&’); var regex = new RegExp(‘[?&]’ + name + ‘(=([^&#]*)|&|#|$)’), results = regex.exec(url); if (!results) return null; if (!results[2]) return ”; return decodeURIComponent(results[2].replace(/\+/g, ‘ ‘)); } The url parameter is optional if […]

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