Categories
JavaScript Answers

How to use JavaScript to detect whether the URL exists before display in iframe?

Sometimes, we want to use JavaScript to detect whether the URL exists before display in iframe.

In this article, we’ll look at how to use JavaScript to detect whether the URL exists before display in iframe.

How to use JavaScript to detect whether the URL exists before display in iframe?

To use JavaScript to detect whether the URL exists before display in iframe, we use fetch.

For instance, we write

const openHelp = async (urlToOpen) => {
  const defaultURL = `http://example.com`;
  const response = await fetch(urlToOpen);

  if (response.status === 200) {
    window.open(urlToOpen, `_blank`);
  } else if (response.status === 404) {
    window.open(defaultURL, `_blank`);
  }
};

to define the openHelp function.

In it, we check if the urlToOpen URL is available with fetch.

We make a get request to it and check if status code.

If it’s 200, then we call window.open to open the urlToOpen URL.

If it’s 404, then we call window.open to open the defaultURL.

Conclusion

To use JavaScript to detect whether the URL exists before display in iframe, we use fetch.

Categories
JavaScript Answers

How to encode a string in JavaScript for displaying in HTML?

Sometimes, we want to encode a string in JavaScript for displaying in HTML.

In this article, we’ll look at how to encode a string in JavaScript for displaying in HTML.

How to encode a string in JavaScript for displaying in HTML?

To encode a string in JavaScript for displaying in HTML, we create a text node.

For instance, we write

document.body.appendChild(document.createTextNode("foo<bar>baz"));

to call createTextNode to create a text node with the text.

Then we call document.body.appendChild to append the text= node as the last child of the body element.

Conclusion

To encode a string in JavaScript for displaying in HTML, we create a text node.

Categories
JavaScript Answers

How to use Node.js MongoDB driver async/await queries with JavaScript?

Sometimes, we want to use Node.js MongoDB driver async/await queries with JavaScript.

In this article, we’ll look at how to use Node.js MongoDB driver async/await queries with JavaScript.

How to use Node.js MongoDB driver async/await queries with JavaScript?

To use Node.js MongoDB driver async/await queries with JavaScript, we use await before database methods.

For instance, we write

const queryDb = async (query) => {
  let db, client;
  try {
    client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, {
      useNewUrlParser: true,
    });
    db = client.db(dbName);
    return await db.collection(collectionName).find(query).toArray();
  } finally {
    client.close();
  }
};

to define the queryDb function.

In it, we call MongoClient.connect to connect to the database with the connect string.

It returns a promise so we use await to get the client.

Then we get the database with client.db.

Then we call find to find an object in the collectionName collection and get the results from the promise returned with await.

We call close to close the connection in the finally block.

Conclusion

To use Node.js MongoDB driver async/await queries with JavaScript, we use await before database methods.

Categories
JavaScript Answers

How to set default array values with JavaScript?

To set default array values with JavaScript, we call the fill method.

For instance, we write

const a = Array(20).fill(0);

to create an empty array with 20 slots with Array.

Then we call fill to fill all the empty slots with 0.

Conclusion

To set default array values with JavaScript, we call the fill method.

Categories
JavaScript Answers

How to remove emoji code using JavaScript?

Sometimes, we want to remove emoji code using JavaScript.

In this article, we’ll look at how to remove emoji code using JavaScript.

How to remove emoji code using JavaScript?

To remove emoji code using JavaScript, we use the replace method.

For instance, we write

const s = "Smile😀".replace(/\p{Emoji}/gu, "");

to call replace on "Smile😀" with the \p{Emoji} pattern to match emojis.

We use the g flag to replace all matches with empty strings.

And we use the u flag to match Unicode characters.

Conclusion

To remove emoji code using JavaScript, we use the replace method.