Categories
JavaScript Answers

How to check if a value is not null and not empty string in JavaScript?

Sometimes, we want to check if a value is not null and not empty string in JavaScript.

In this article, we’ll look at how to check if a value is not null and not empty string in JavaScript.

How to check if a value is not null and not empty string in JavaScript?

To check if a value is not null and not empty string in JavaScript, we can use the !== operator.

For instance, we write

if (data !== null && data !== "") {
  // ...
}

to check if data isn’t null and data isn’t an empty string with

data !== null && data !== ""

Conclusion

To check if a value is not null and not empty string in JavaScript, we can use the !== operator.

Categories
JavaScript Answers

How to iterate over a JavaScript associative array in sorted order?

Sometimes, we want to iterate over a JavaScript associative array in sorted order.

In this article, we’ll look at how to iterate over a JavaScript associative array in sorted order.

How to iterate over a JavaScript associative array in sorted order?

To iterate over a JavaScript associative array in sorted order, we can get the keys of the object with Object.keys as an array.

Then we call the sort method on the array.

For instance, we write

const sortedKeys = Object.keys(a).sort();

to get the keys of object a as an array with Object.keys.

Then we call sort to return an array that sort the array of keys alphabetically.

Conclusion

To iterate over a JavaScript associative array in sorted order, we can get the keys of the object with Object.keys as an array.

Categories
JavaScript Answers

How to simulate a click by using x, y coordinates in JavaScript?

Sometimes, we want to simulate a click by using x, y coordinates in JavaScript.

In this article, we’ll look at how to simulate a click by using x, y coordinates in JavaScript.

How to simulate a click by using x, y coordinates in JavaScript?

To simulate a click by using x, y coordinates in JavaScript, we can call the initMouseEvent method.

For instance, we write

const click = (x, y) => {
  const ev = document.createEvent("MouseEvent");
  const el = document.elementFromPoint(x, y);
  ev.initMouseEvent(
    "click",
    true /* bubble */,
    true /* cancelable */,
    window,
    null,
    x,
    y,
    0,
    0 /* coordinates */,
    false,
    false,
    false,
    false /* modifier keys */,
    0 /*left*/,
    null
  );
  el.dispatchEvent(ev);
};

to create the mouse event object by calling the createElement method with 'MouseEvent'.

Then we call elementFromPoint to get the element that we want to click on with the x and y coordinates of the screen.

Next we call initMouseEvent with a few arguments to initialize the mouse event.

'click' triggers a click.

Then we call it with other arguments to set some options described in the comments.

Finally, we call dispatchEvent to trigger the mouse event.

Conclusion

To simulate a click by using x, y coordinates in JavaScript, we can call the initMouseEvent method.

Categories
JavaScript Answers

How to make a non-cached request with fetch and JavaScript?

Sometimes, we want to make a non-cached request with fetch and JavaScript.

In this article, we’ll look at how to make a non-cached request with fetch and JavaScript.

How to make a non-cached request with fetch and JavaScript?

To make a non-cached request with fetch and JavaScript, we can set the pragma and cache-control request headers.

For instance, we write

const myImage = document.querySelector("img");

const headers = new Headers();
myHeaders.append("pragma", "no-cache");
myHeaders.append("cache-control", "no-cache");

const myInit = {
  method: "GET",
  headers,
};

const myRequest = new Request("myImage.jpg");

const response = await fetch(myRequest, myInit);
const blob = await response.blob();
const objectURL = URL.createObjectURL(blob);
myImage.src = objectURL;

to call fetch with myRequest and myInit.

In myInit, we set the headers to the headers object.

We set the pragma header to 'no-cache' and cache-control to 'no-cache with append to make a non-cached request.

Then we get the blob from the response with blob.

Conclusion

To make a non-cached request with fetch and JavaScript, we can set the pragma and cache-control request headers.

Categories
JavaScript Answers

How to listen for scroll event for iPhone or iPad with JavaScript?

Sometimes, we want to listen for scroll event for iPhone or iPad with JavaScript.

In this article, we’ll look at how to listen for scroll event for iPhone or iPad with JavaScript.

How to listen for scroll event for iPhone or iPad with JavaScript?

To listen for scroll event for iPhone or iPad with JavaScript, we can listen to the scroll event.

For instance, we write

window.addEventListener("scroll", () => {
  console.log("Scrolled");
});

window.onscroll = () => {
  console.log("Scrolled");
};

to call window.addEventListener to add the scroll event listener.

The callback runs when we scroll on our iPhone or iPad.

Likewise, we set window.onscroll to a function that runs when we scroll to add a scroll event listener.

Conclusion

To listen for scroll event for iPhone or iPad with JavaScript, we can listen to the scroll event.