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 instead: function consolelog(v){ if (window.console && window.console.log){ window.console.log(v); } } However this requires all developers

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 used to do just that, and the logic itself can be adapted to be used

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 pattern javascript object which will show an alert window displaying “hello world”. var MyFunction =

What is cache busting?

A cache-buster is a unique string which is appended to a URL in the form of a query string. It is generally not read by the server-side and is used purely to form a unique URL. For example: /Scripts/foo.js?v=1 This is often used on client side files such as Javascript, CSS and Images in order

Quickly create HTML elements in jsFiddle

In case you haven’t already heard of it, jsFiddle is a web application which allows you to enter HTML, CSS and Javascript into different windows and render the output. It’s a great tool for testing various front-end concepts, and then “fiddles” can be saved and shared. Because of this feature it’s a popular tool on

Abstract config settings from web.config

In ASP.NET, application settings can be specified in the web.config file such as: <appSettings> <add key="PostsPerPage" value="6" /> <add key="CookieName" value="Foo" /> </appSettings> The beauty of this is that you don’t need to edit code to make changes to the application, and a single pre-compiled application can be used across multiple environments with different settings.

Copy table schema to new a table

In SQL Server, SELECT INTO is used to copy data from an existing table/s into a new one. However by adding a WHERE clause which will always return false, this will prevent any data from being copied, and therefore create an empty copy of the table schema into a new table. For example if we

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 on and off page title notifications. To activate the page title notification call the following:

Namespacing jQuery event handlers

If we are to attach 2 or more event handlers to an element these will become stacked and run linear in the order they were attached. For example: $(".foo").on("click", function(e){ console.log("bar"); }); $(".foo").on("click", function(e){ console.log("baz"); }); If .foo is then clicked, the browser console will log: "bar" "baz" If we wish to remove these event

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 0 as one of the parameters: var i = -45; i = Math.max(0,i); console.log(i); //0