JavaScript

Tips and tricks I learned throughout the years.

JavaScript is different. Experienced users that come from other languages have a hard time learning it, because it looks similar but operates differently. Here I will share all the insights I learned in the past few years. You will find it quite useful whether you programmed for years, or are just getting started.

JavaScript tips — Page 4

My variables are set to the last index in JavaScript

JavaScript is known to host some quirky behavior. Most of the time however, all it takes is getting familiar with it to understand what is really happening and how to work with it. New and experience…

Read

Calling a function out of scope

With the advent of jQuery, coding in JavaScript became accessible to almost everybody. But with it came some very fundamental errors that could have been avoided if the person read the very basic of j…

Read

Get computed style

Sometimes you want to get the width of an element, and the obvious way of getting it does not work. element.style.width; // (empty string) Yeah, I know it should be this easy but it is not. Instea…

Read

Accessing URL parameters in JavaScript

I have worked with PHP extensively and it is very convenient to have all the URL parameters automatically assigned to the global array $_GET. Here is an example: http://www.example.org/?foo=bar $_GET…

Read

Calling a function that has no name

You can make use of recursion easily in JavaScript. Let's try it with factorials: function factorial(num){ if (num < 0) { return -1; } else if (num == 0) { return 1; …

Read

Making fast cross-domain requests when the browser is leaving

There are times you want to make a very quick web request and don't care about the response looks like. It is possible to make requests using Ajax, but we still get a response. One trick you will see…

Read

Making a HTTP request before the page redirects

There are many ways to track when a user clicks on a link. It seems simple enough but there is something that people always forget and their code doesn't run. Let me show you an example. Click me //…

Read

How to detect when the images are loaded

It is simple to load images to the page using JavaScript. We can create it using two methods then append it to the DOM. var img1 = document.createElement("img"); var img2 = new Image(); img1.src = "…

Read

Photography