Categories
HTML

How to cache an image in HTML?

Sometimes, we want to cache an image in HTML.

In this article, we’ll look at how to cache an image in HTML.

How to cache an image in HTML?

To cache an image in HTML, we can add a link element.

For instance, we write

<link rel="preload" href="bg-image-wide.png" as="image" />

to add a link element that has the href attribute set to the URL of the image to cache.

And we set the rel attribute to preload to preload the image.

Conclusion

To cache an image in HTML, we can add a link element.

Categories
Vue Answers

How to access external JSON file objects in Vue.js app?

Sometimes, we want to access external JSON file objects in Vue.js app.

In this article, we’ll look at how to access external JSON file objects in Vue.js app.

How to access external JSON file objects in Vue.js app?

To access external JSON file objects in Vue.js app, we can import the JSON with import and then use that as the value of a reactive property.

For instance, we write

<template>
  <div>
    <div v-for="data in myJson">{{ data }}</div>
  </div>
</template>

<script>
import json from "./json/data.json";

export default {
  data() {
    return {
      myJson: json,
    };
  },
};
</script>

to import the json array from the ./json/data.json file.

And then we set json as the value of the myJson reactive property.

Next, in the template, we loop through the myJson object and show the data of each entry.

Conclusion

To access external JSON file objects in Vue.js app, we can import the JSON with import and then use that as the value of a reactive property.

Categories
JavaScript Answers

How to convert Markdown to HTML with JavaScript?

Sometimes, we want to convert Markdown to HTML with JavaScript.

In this article, we’ll look at how to convert Markdown to HTML with JavaScript.

How to convert Markdown to HTML with JavaScript?

To convert Markdown to HTML with JavaScript, we can use the showdown library.

For instance, we write

<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"></script>

to add the showdown library with a script tag.

Then we write

const converter = new showdown.Converter();
const text = "# hello, markdown!";
const html = converter.makeHtml(text);

to create a showdown.Converter convert object.

Then we call makeHtml on it with the Markdown text to convert it to html.

Conclusion

To convert Markdown to HTML with JavaScript, we can use the showdown library.

Categories
JavaScript Answers

How to create a JavaScript variable reference or alias?

Sometimes, we want to create a JavaScript variable reference or alias.

In this article, we’ll look at how to create a JavaScript variable reference or alias.

How to create a JavaScript variable reference or alias?

To create a JavaScript variable reference or alias, we can create an object.

For instance, we write

const obj = { x: 1 };
const aliasToObj = obj;
aliasToObj.x++;
alert(obj.x);

to create the obj object and assign obj to aliasToObj.

aliasToObj references obj, so when we update its value of x with aliasToObj.x++.

obj.x also changes value.

Therefore, aliasToObj.x and obj.x are both 2.

Conclusion

To create a JavaScript variable reference or alias, we can create an object.

Categories
JavaScript Answers

How to get file name when user select a file from the file input with JavaScript?

Sometimes, we want to get file name when user select a file from the file input with JavaScript.

In this article, we’ll look at how to get file name when user select a file from the file input with JavaScript.

How to get file name when user select a file from the file input with JavaScript?

To get file name when user select a file from the file input with JavaScript.

For instance, we write

document.getElementById("fileInput").onchange = (e) => {
  console.log(e.target.value);
};

to select the file input with getElementById.

Then we set its onchange property to a function that get the selected file’s name with e.target.value.

Conclusion

To get file name when user select a file from the file input with JavaScript.