Category Archives: Javascript/J-Query

Read cookies (JavaScript)

Simplest way:

const getCookieValueByRegEx = (name) => (document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || '');
this.lang = getCookieValueByRegEx(mycookiename);

Various ways to read cookies with vanilla javaScript.

document.cookie="testCookie=Foo Bar; expires=Thu, 01 Dec 2099 12:00:00 UTC; path=/";
document.cookie="anotherTestCookie=Baz Qux; expires=Thu, 01 Dec 2099 12:00:00 UTC; path=/";

function getCookieValueByRegEx(a, b) {
  b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
  return b ? b.pop() : '';
}

function getCookieValueByArrayFunctions(a, b, c) {
  b = '; ' + document.cookie;
  c = b.split('; ' + a + '=');
  return !!(c.length - 1) ? c.pop().split(';').shift() : '';
}

function getCookieValueByLoop(a, b, c, d, e) {
  b = document.cookie.split('; ');
  for (e = b.length - 1; e >= 0; e--) {
    c = b[e].split('=');
    if (a === c[0]) return c[1];
  }
  return '';
}

Links:

Submit form data to call a web service (Javascript)

If you want to create a small web project, a approach would be to use a combination of bootstrap (frontend), lumen (backend) and jquery (interaction between the two). It is a very strong combination with a very low owning and maintenance cost that can easily scale (by upgrading to laravel) if needed. You can also use angular with does not add a lot of overhead.

While you are trying to implement the above, the following pattern is encountered a lot of times: you need to call a web service via js while the data are filled by the user via a form on the html page. While the web service is being called, you need to show a spinner on the page so that the user understands that he needs to wait until the call has been completed.

Continue reading Submit form data to call a web service (Javascript)

Round float numbers in Javascript

Why it’s complicated

JavaScript’s Math object provides a method for rounding to whole numbers. If we want to round to a set number of decimal places, then we have to handle that ourselves. This post doesn’t get into the details of floating-point arithmetic, but the short of it is most programming languages use a binary floating-point representation which can only approximate many decimal fractions. This results in rounding errors for the most common approaches to rounding in JavaScript.

Rounding Errors

The most common solutions for rounding to a decimal place is to either use Number.prototype.toFixed(), or multiply the float by some power of 10 in order to leverage Math.round(). Both of these work, except sometimes a decimal of 5 is rounded down instead of up.

Number((1.005).toFixed(2)); // 1 instead of 1.01
Math.round(1.005*100)/100; // 1 instead of 1.01

A Better Solution

The rounding problem can be avoided by using numbers represented in exponential notation:

Number(Math.round(1.005+'e2')+'e-2'); // 1.01

And to abstract that into something more usable:

function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

round(1.005, 2); // 1.01

References:

http://www.jacklmoore.com/notes/rounding-in-javascript/