Curtis Timson

Front End & .NET Developer

Category

Javascript

Default parameters with ES6

With ES5 if you wish to set default parameters on a javascript function you would be required to check the value and supply a default if one hadn’t been set. var myFunction = function(arg1, arg2){ arg1 = arg1 || 25;… Continue Reading →

Preventing console.log issues in IE

In some older browsers, including versions of Internet Explorer, window.console doesn’t exist and therefore calling console.log() will result in an error. One way of fixing this is to create a function which checks whether console exists and use this function… Continue Reading →

Round to the nearest decimal

If we need round to the nearest whole number, we can use the Math.round() function to return this: Math.round(1.34234); //returns 1 However what if we need to round to a decimal place? For example 0.5. The following function can be… Continue Reading →

Mixing Revealing Module and Singleton Javascript Patterns

Until recently I’ve been using the Singleton pattern for my javascript objects. This works well as my functions are namespaced and therefore the risk of clashing object names across Javascript files is reduced. For this example I’ve created a singleton… Continue Reading →

Create a flashing tab notification page title

Page title notifications switch between the default page title and a notification message continously in order to grab the user’s attention. This is commonly used with chat applications. I’ve written a small javascript object which can be used to switch… Continue Reading →

Default negative variables to zero in javascript

If you need to default a value to 0 if its negative, you could do: var i = -45; if (i<0){ i = 0; } console.log(i); //0 However, a shorter way of doing this would be to use Math.max() passing… Continue Reading →

console.log() vs alert() when debugging

Using console.log() is a great way of getting quick results about what variables are holding what values at which point, and which areas of code are being called, without having to debug. Before javascript debugging became so accessible, one of… Continue Reading →

Reading URL hashtag values

URL hashtags were primarily used in order to position the page scroll on load to a particular element. With javascript we can access what the value of the hashtag is on page load. if (window.location.hash){ var hash = window.location.hash.substring(1); alert(“hashtag… Continue Reading →

Check javascript function exists before calling it

On large applications, there may be some instances where you need to make a call to a function which belongs in a different javascript file. There might also be a case where this javascript file isn’t always included on every… Continue Reading →

© 2017 Curtis Timson — Powered by WordPress

Theme by Anders NorenUp ↑