Categories
JavaScript Answers

How to use window.postMessage across domains with JavaScript?

Sometimes, we want to use window.postMessage across domains with JavaScript.

In this article, we’ll look at how to use window.postMessage across domains with JavaScript.

How to use window.postMessage across domains with JavaScript?

To use window.postMessage across domains with JavaScript, we call top.postMessage in our iframe.

For instance, we write

top.postMessage("hello", "A");

in our iframe to call postMessage to send data to the parent page.

Then we write

window.addEventListener(
  "message",
  (e) => {
    if (e.origin !== "B") {
      return;
    }
    alert(e.data);
  },
  false
);

in our parent page to listen for the message event.

In the callback, we check the domain the event is originated from with e.origin.

And we get the arguments passed into postMessage with e.data.

Conclusion

To use window.postMessage across domains with JavaScript, we call top.postMessage in our iframe.

Categories
JavaScript Answers

How to fill an input field using Puppeteer and JavaScript?

Sometimes, we want to fill an input field using Puppeteer and JavaScript.

In this article, we’ll look at how to fill an input field using Puppeteer and JavaScript.

How to fill an input field using Puppeteer and JavaScript?

To fill an input field using Puppeteer and JavaScript, we can use the page.keyword.type method.

For instance, we write

await page.focus("#email");
await page.keyboard.type("test");

to focus on the input with ID email with

await page.focus("#email");

Then we type in the input we focused on with

await page.keyboard.type("test");

Conclusion

To fill an input field using Puppeteer and JavaScript, we can use the page.keyword.type method.

Categories
JavaScript Answers

How to replace callbacks with promises in Node.js and JavaScript?

Sometimes, we want to replace callbacks with promises in Node.js and JavaScript.

In this article, we’ll look at how to replace callbacks with promises in Node.js and JavaScript.

How to replace callbacks with promises in Node.js and JavaScript?

To replace callbacks with promises in Node.js and JavaScript, we can use the promisfy function from the util module.

For instance, we write

const { promisify } = require("util");
const glob = promisify(require("glob"));

app.get("/", async (req, res) => {
  const files = await glob("src/**/*-spec.js");
  res.render("template-test", { files });
});

to call promisify with the glob function we get from require("glob").

promisify returns a promise if we pass in anything that takes a standard Node.js async callback.

Therefore, we can use await with glob to get the resolve value from the promise.

Conclusion

To replace callbacks with promises in Node.js and JavaScript, we can use the promisfy function from the util module.

Categories
JavaScript Answers

How to compare JavaScript Date objects?

Sometimes, we want to compare JavaScript Date objects.

In this article, we’ll look at how to compare JavaScript Date objects.

How to compare JavaScript Date objects?

To compare JavaScript Date objects, we convert the dates to timestamps before we compare them.

For instance, we write

console.log(+startDate2 === +startDate3);
console.log(startDate2.getTime() === startDate3.getTime());
console.log(Number(startDate2) === Number(startDate3));

to use +, getTime or Number to convert the startDate2 and startDate3 date objects into timestamps in milliseconds before we compare them.

Timestamps are integers, so we can compare them with the === operator.

Conclusion

To compare JavaScript Date objects, we convert the dates to timestamps before we compare them.

Categories
JavaScript Answers

How to parse date time string in dd.mm.yyyy format with JavaScript?

Sometimes, we want to parse date time string in dd.mm.yyyy format with JavaScript.

In this article, we’ll look at how to parse date time string in dd.mm.yyyy format with JavaScript.

How to parse date time string in dd.mm.yyyy format with JavaScript?

To parse date time string in dd.mm.yyyy format with JavaScript, we can call the string split method.

For instance, we write

const strDate = "03.09.1979";
const [day, month, year] = strDate.split(".");
const date = new Date(+year, +month - 1, +day);

to call strDate.split to split the strDate string by the period.

Then we get the day, month, and year from the split string.

Next, we pass the 3 values into the Date constructor to create a date object from them.

We subtract month by 1 before we pass it into the Date constructor since it accepts a month that starts with 0 for January.

We use + to convert each value to a number before we pass them in as arguments.

Conclusion

To parse date time string in dd.mm.yyyy format with JavaScript, we can call the string split method.