Categories
JavaScript Answers

How to pass a URL with multiple parameters into a URL with JavaScript?

Sometimes, we want to pass a URL with multiple parameters into a URL with JavaScript.

In this article, we’ll look at how to pass a URL with multiple parameters into a URL with JavaScript.

How to pass a URL with multiple parameters into a URL with JavaScript?

To pass a URL with multiple parameters into a URL with JavaScript, we use the encodeURIComponent method.

For instance, we write

const encodedParam = encodeURIComponent(
  "www.foobar.com/?first=1&second=12&third=5"
);

to call encodeURIComponent with the URL string.

Then URL encoded string is returned.

Conclusion

To pass a URL with multiple parameters into a URL with JavaScript, we use the encodeURIComponent method.

Categories
JavaScript Answers

How to use address instead of longitude and latitude with Google Maps API and JavaScript?

Sometimes, we want to use address instead of longitude and latitude with Google Maps API and JavaScript.

In this article, we’ll look at how to use address instead of longitude and latitude with Google Maps API and JavaScript.

How to use address instead of longitude and latitude with Google Maps API and JavaScript?

To use address instead of longitude and latitude with Google Maps API and JavaScript, we can use geocoding.

For instance, we write

const addressesArray = [
  "Address Str.No, Postal Area/city",
  //...
];
const map = new google.maps.Map(document.getElementById("map"), {
  center: {
    lat: 12.7826,
    lng: 105.0282,
  },
  zoom: 6,
  gestureHandling: "cooperative",
});

const geocoder = new google.maps.Geocoder();
for (const address of addressArray) {
  geocoder.geocode(
    {
      address,
    },
    (results, status) => {
      if (status === "OK") {
        const marker = new google.maps.Marker({
          map,
          position: results[0].geometry.location,
          center: {
            lat: 12.7826,
            lng: 105.0282,
          },
        });
      } else {
        cinsole.log(
          "Geocode was not successful for the following reason: ",
          status
        );
      }
    }
  );
}

to create a map with the google.maps.Map constructor.

Then we create a Geocoder object.

Next we loop through the addressArray with a for-of loop.

In it, we call geocoder.geocode with an object with the address property to to get the location with results[0].geometry.location.

Conclusion

To use address instead of longitude and latitude with Google Maps API and JavaScript, we can use geocoding.

Categories
JavaScript Answers

How to rotate image with JavaScript?

Sometimes, we want to rotate image with JavaScript.

In this article, we’ll look at how to rotate image with JavaScript.

How to rotate image with JavaScript?

To rotate image with JavaScript, we set the transform property.

For instance, we write

document.getElementById("image").style.transform = "rotate(90deg)";

to select the image with getElementById.

Then we set its style.transform property to "rotate(90deg)" to rotate the image by 90 degrees.

Conclusion

To rotate image with JavaScript, we set the transform property.

Categories
JavaScript Answers

How to convert JavaScript date to string?

Sometimes, we want to convert JavaScript date to string.

In this article, we’ll look at how to convert JavaScript date to string.

How to convert JavaScript date to string?

To convert JavaScript date to string, we use some date methods.

For instance, we write

const temp = new Date();
const dateStr =
  temp.getFullYear().toString().padStart(2, "0") +
  (1 + temp.getMonth()).toString().padStart(2, "0") +
  temp.getDate().toString().padStart(2, "0") +
  temp.getHours().toString().padStart(2, "0") +
  temp.getMinutes().toString().padStart(2, "0") +
  temp.getSeconds().toString().padStart(2, "0");

to get the 4 digit year with getFullYear.

We get the month with 0 for January to 11 for December with getMonth.

We get the date with getDate.

We get the hours with getHours.

We get the minutes with getMinutes.

And we get the seconds with getSeconds.

We convert each number to a string with toString.

Then we call padStart to prepend '0' to each string until it has length 2.

And then we concatenate them all together.

Conclusion

To convert JavaScript date to string, we use some date methods.

Categories
Vue Answers

How to run code in mounted and again for restart functionality with Vue.js?

Sometimes, we want to run code in mounted and again for restart functionality with Vue.js.

In this article, we’ll look at how to run code in mounted and again for restart functionality with Vue.js.

How to run code in mounted and again for restart functionality with Vue.js?

To run code in mounted and again for restart functionality with Vue.js, we move the code in mounted into its own method.

For instance, we write

new Vue({
  methods: {
    init() {
      //...
    },
  },
  mounted() {
    this.init();
  },
});

to define the init method and call it in mounted.

Then we add

<button v-if="playerWon" @click="init">Play Again</button>

into our HTML code to call init when we click on the button.

Conclusion

To run code in mounted and again for restart functionality with Vue.js, we move the code in mounted into its own method.