Categories
JavaScript Answers

How to display an image stored as byte array in HTML and JavaScript?

Sometimes, we want to display an image stored as byte array in HTML and JavaScript.

In this article, we’ll look at how to display an image stored as byte array in HTML and JavaScript.

How to display an image stored as byte array in HTML and JavaScript?

To display an image stored as byte array in HTML and JavaScript, we can convert the byte array to a base64 URL string.

For instance, we add an img element with

<img id="itemPreview" src="" />

Then we write

const blob = new Blob([uint8ArrayBuffer], { type: "image/jpeg" });
const imageUrl = URL.createObjectURL(blob);
document.getElementById("itemPreview").src = imageUrl;

to select the img element with getElementById.

Then we set the src property to the base64 URL with the byte array converted to a base64 string.

We get the base64 image URL from by creating the uint8ArrayBuffer to a blob with the Blob constructor.

Then we call URL.createObjectURL to convert the blob to a base64 string.

Conclusion

To display an image stored as byte array in HTML and JavaScript, we can convert the byte array to a base64 URL string.

Categories
JavaScript Answers

How to automatically open default email client and pre-populate content with JavaScript?

Sometimes, we want to automatically open default email client and pre-populate content with JavaScript.

In this article, we’ll look at how to automatically open default email client and pre-populate content with JavaScript.

How to automatically open default email client and pre-populate content with JavaScript?

To automatically open default email client and pre-populate content with JavaScript, we can navigate to the mailto URL with the email subject and body filled in.

For instance, we write

const email = message.emailId;
const subject = message.subject;
const emailBody = "Hi " + message.from;
document.location =
  "mailto:" + email + "?subject=" + subject + "&body=" + emailBody;

to set document.location to a mailto URL string with the subject and body of the email.

This will make the browser open the default mail client with the subject and body of the email filled in with the email address.

Conclusion

To automatically open default email client and pre-populate content with JavaScript, we can navigate to the mailto URL with the email subject and body filled in.

Categories
JavaScript Answers

How to save session only cookies with JavaScript?

Sometimes, we want to save session only cookies with JavaScript.

In this article, we’ll look at how to save session only cookies with JavaScript.

How to save session only cookies with JavaScript?

To save session only cookies with JavaScript, we can use session storage instead of cookies.

For instance, we write

const myVariable = { a: [1, 2, 3, 4], b: "some text" };

sessionStorage["myVariable"] = JSON.stringify(myVariable);
const readValue = JSON.parse(sessionStorage["myVariable"]);

to save an entry with key myVariable into session storage with

sessionStorage["myVariable"] = JSON.stringify(myVariable);

Since myVariable is an object, we’ve convert it to a string with JSON.stringify before we can save the value.

Then we read the value by the key with

sessionStorage["myVariable"]

Then we call JSON.parse to parse the stringified JSON object into a JavaScript object.

Conclusion

To save session only cookies with JavaScript, we can use session storage instead of cookies.

Categories
JavaScript Answers

How to convert relative path to absolute with Node.js?

Sometimes, we want to convert relative path to absolute with Node.js.

In this article, we’ll look at how to convert relative path to absolute with Node.js.

How to convert relative path to absolute with Node.js?

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

For instance, we write

const { resolve } = require("path");

const absPath = resolve("../../bb/tmp.txt");

to call resolve with a relative path to return a string with the equivalent absolute path.

Conclusion

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

Categories
JavaScript Answers

How to add Sequelize table without column ‘id’ with JavaScript?

Sometimes, we want to add Sequelize table without column ‘id’ with JavaScript.

In this article, we’ll look at how to add Sequelize table without column ‘id’ with JavaScript.

How to add Sequelize table without column ‘id’ with JavaScript?

To add Sequelize table without column ‘id’ with JavaScript, we define a table with our own primary key column.

For instance, we write

const AcademyModule = sequelize.define(
  "academy_module",
  {
    academy_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
    module_id: DataTypes.INTEGER,
    module_module_type_id: DataTypes.INTEGER,
    sort_number: DataTypes.INTEGER,
    requirements_id: DataTypes.INTEGER,
  },
  {
    freezeTableName: true,
  }
);

to call sequelize.define with the table name and an object with the column names and their types.

We add the academy_id primary key column so the id column won’t be added automatically.

Conclusion

To add Sequelize table without column ‘id’ with JavaScript, we define a table with our own primary key column.