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
React Answers

How to get selected option text using React?

Sometimes, we want to get selected option text using React.

In this article, we’ll look at how to get selected option text using React.

How to get selected option text using React?

To get selected option text using React, we use the properties in the event object.

For instance, we write

const { options, selectedIndex } = e.target;
console.log(options[selectedIndex].innerHTML);

in the change event handler to get the options and selectedIndex properties from the e.target drop down.

options has all the option elements in the drop down.

selectedIndex has the index of the option selected.

Then we get the option selected with options[selectedIndex].

And we get its content with innerHTML.

Conclusion

To get selected option text using React, we use the properties in the event object.

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
React Answers

How to navigate on path by button click in React Router v4?

Sometimes, we want to navigate on path by button click in React Router v4.

In this article, we’ll look at how to navigate on path by button click in React Router v4.

How to navigate on path by button click in React Router v4?

To navigate on path by button click in React Router v4, we use the useHistory hook.

For instance, we write

import React from "react";
import { useHistory } from "react-router-dom";

export default () => {
  const history = useHistory();

  return <button onClick={() => history.push("/your/path")}>Click me</button>;
};

to call useHistory to get the history object.

Then we call history.push with the path we want to go to in the click handler of the button.

Conclusion

To navigate on path by button click in React Router v4, we use the useHistory hook.

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.