Categories
JavaScript Answers

How to call JavaScript function from URL or address bar?

Sometimes, we want to call JavaScript function from URL or address bar.

In this article, we’ll look at how to call JavaScript function from URL or address bar.

How to call JavaScript function from URL or address bar?

To call JavaScript function from URL or address bar, we can type in a data URL.

For instance, we type in

data:text/html,<script>alert('hi');</script>

into the address bar to run the JavaScript code in the script tag.

Therefore, we see an alert box that shows ‘hi’ displayed when we type this in.

Conclusion

To call JavaScript function from URL or address bar, we can type in a data URL.

Categories
JavaScript Answers

How to replace a regex substring match in JavaScript?

Sometimes, we want to replace a regex substring match in JavaScript.

In this article, we’ll look at how to replace a regex substring match in JavaScript.

How to replace a regex substring match in JavaScript?

To replace a regex substring match in JavaScript, we can use the string replace method with a regex.

For instance, we write

let str = "asd-0.testing";
const regex = /\d/;
str = str.replace(regex, "1");
console.log(str);

to call str.replace with regex and 1 to replace the digits in str with 1.

We use \d to match a single digit in the string.

Conclusion

To replace a regex substring match in JavaScript, we can use the string replace method with a regex.

Categories
JavaScript Answers

How to detect when a window is resized using JavaScript?

Sometimes, we want to detect when a window is resized using JavaScript.

In this article, we’ll look at how to detect when a window is resized using JavaScript.

How to detect when a window is resized using JavaScript?

To detect when a window is resized using JavaScript, we listen for the resize event on window.

For instance, we write

window.addEventListener("resize", onResize);

to call window.addEventListener to listen to the resize event triggered by window.

The onResize function is the resize event listener and it runs every time the window is resized.

Conclusion

To detect when a window is resized using JavaScript, we listen for the resize event on window.

Categories
JavaScript Answers

How to go from Blob to ArrayBuffer with JavaScript?

Sometimes, we want to go from Blob to ArrayBuffer with JavaScript.

In this article, we’ll look at how to go from Blob to ArrayBuffer with JavaScript.

How to go from Blob to ArrayBuffer with JavaScript?

To go from Blob to ArrayBuffer with JavaScript, we can use the Response constructor.

For instance, we write

const arrayBuffer = await new Response(blob).arrayBuffer();

to create a Response object from blob.

And then we call arraybuffer to return a promise with the array buffer converted from the blob.

Conclusion

To go from Blob to ArrayBuffer with JavaScript, we can use the Response constructor.

Categories
JavaScript Answers

How to resize image with JavaScript canvas?

Sometimes, we want to resize image with JavaScript canvas.

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

How to resize image with JavaScript canvas?

To resize image with JavaScript canvas, we call drawImage with the given image width and height.

For instance, we write

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const img = new Image();

img.onload = () => {
  canvas.height = canvas.width * (img.height / img.width);

  const oc = document.createElement("canvas");
  const octx = oc.getContext("2d");
  oc.width = img.width * 0.5;
  oc.height = img.height * 0.5;
  octx.drawImage(img, 0, 0, oc.width, oc.height);

  octx.drawImage(oc, 0, 0, oc.width * 0.5, oc.height * 0.5);

  ctx.drawImage(
    oc,
    0,
    0,
    oc.width * 0.5,
    oc.height * 0.5,
    0,
    0,
    canvas.width,
    canvas.height
  );
};
img.src = "https://picsum.photos/200/300";

to set the img.onload property to a function that runs when the image is loaded.

In it, we set the canvas height and width with

canvas.height = canvas.width * (img.height / img.width);

We resize the the image to 50% of its original size with

const oc = document.createElement("canvas");
const octx = oc.getContext("2d");
oc.width = img.width * 0.5;
oc.height = img.height * 0.5;
octx.drawImage(img, 0, 0, oc.width, oc.height);

Then we resize it to its final size with

ctx.drawImage(
  oc,
  0,
  0,
  oc.width * 0.5,
  oc.height * 0.5,
  0,
  0,
  canvas.width,
  canvas.height
);

We draw the resized image by drawing the image in the oc canvas width 50% of the size of the oc canvas.

Conclusion

To resize image with JavaScript canvas, we call drawImage with the given image width and height.