Categories
JavaScript Answers

How to JSON stringify a JavaScript Date and preserve time zone?

Sometimes, we want to JSON stringify a JavaScript Date and preserve time zone.

In this article, we’ll look at how to JSON stringify a JavaScript Date and preserve time zone.

How to JSON stringify a JavaScript Date and preserve time zone?

To JSON stringify a JavaScript Date and preserve time zone, we can create our own replacer function.

For instance, we write

const replacer = function (key, value) {
  if (this[key] instanceof Date) {
    return this[key].toUTCString();
  }

  return value;
};

console.log(JSON.stringify(new Date(), replacer));
console.log(JSON.stringify({ myProperty: new Date() }, replacer));

to define the replacer function.

In it, we check if the this[key] property is a Date.

this is the object being stringified.

If it is, then we stringify it as a UTC string with toUTCString.

Otherwise, we return value as is.

Then we call JSON.stringify with values we want to convert to JSON strings and use replacer to do stringify the properties or values.

Conclusion

To JSON stringify a JavaScript Date and preserve time zone, we can create our own replacer function.

Categories
JavaScript Answers

How to pass a reference of a variable that is immutable as argument in a function with JavaScript?

Sometimes, we want to pass a reference of a variable that is immutable as argument in a function with JavaScript.

In this article, we’ll look at how to pass a reference of a variable that is immutable as argument in a function with JavaScript.

How to pass a reference of a variable that is immutable as argument in a function with JavaScript?

To pass a reference of a variable that is immutable as argument in a function with JavaScript, we can make the function accept an object.

For instance, we write

const x = { value: 0 };

const a = (obj) => {
  obj.value++;
};

a(x);
console.log(x.value);

to define the x object and a function.

In a, we increment the value property by 1.

Then we call a with x.

As a result, x.value is 1 since objects are passed by reference.

Conclusion

To pass a reference of a variable that is immutable as argument in a function with JavaScript, we can make the function accept an object.

Categories
JavaScript Answers

How to detect if browser supports HTML5 Local Storage with JavaScript?

Sometimes, we want to detect if browser supports HTML5 Local Storage with JavaScript.

In this article, we’ll look at how to detect if browser supports HTML5 Local Storage with JavaScript.

How to detect if browser supports HTML5 Local Storage with JavaScript?

To detect if browser supports HTML5 Local Storage with JavaScript, we check if Storage is defined.

For instance, we write

if (typeof Storage !== "undefined") {
  // ...
} else {
  // ...
}

to check if Storage isn’t undefined with typeof Storage !== "undefined".

If it’s true, then local storage is available.

Otherwise, it’s not available.

Conclusion

To detect if browser supports HTML5 Local Storage with JavaScript, we check if Storage is defined.

Categories
JavaScript Answers

How to replace CSS file on the fly and apply the new style to the page with JavaScript?

Sometimes, we want to replace CSS file on the fly and apply the new style to the page with JavaScript.

In this article, we’ll look at how to replace CSS file on the fly and apply the new style to the page with JavaScript.

How to replace CSS file on the fly and apply the new style to the page with JavaScript?

To replace CSS file on the fly and apply the new style to the page with JavaScript, we can set the media attribute of the link element.

For instance, we write

<link rel="stylesheet" href="main.css" />
<link rel="stylesheet" href="light.css" media="none" id="light" />
<link rel="stylesheet" href="dark.css" media="none" id="dark" />

to add a few style sheets.

Then we write

const enableStylesheet = (node) => {
  node.media = "";
};

const disableStylesheet = (node) => {
  node.media = "none";
};

to define the enableStylesheet and disableStylesheet functions.

We set node.media to an empty string in enableStylesheet to enable the stylesheet.

And we set node.media to 'none' in disableStylesheet to disable the stylesheet.

Conclusion

To replace CSS file on the fly and apply the new style to the page with JavaScript, we can set the media attribute of the link element.

Categories
JavaScript Answers

How to offset the center point in Google Maps API V3 and JavaScript?

Sometimes, we want to offset the center point in Google Maps API V3 and JavaScript.

In this article, we’ll look at how to offset the center point in Google Maps API V3 and JavaScript.

How to offset the center point in Google Maps API V3 and JavaScript?

To offset the center point in Google Maps API V3 and JavaScript, we can use the panTo method.

For instance, we write

const offsetX = 0.25;
const offsetY = 0.25;

const span = map.getBounds().toSpan();

const newCenter = {
  lat: center.lat() + span.lat() * offsetY,
  lng: center.lng() + span.lng() * offsetX,
};

map.panTo(newCenter);

to get the latitude and longitude from center with lat and lng respectively.

Then we set the new lat and lng in the newCenter object.

Next, we call panTo with newCenter to pan the map to the new center coordinates.

Conclusion

To offset the center point in Google Maps API V3 and JavaScript, we can use the panTo method.