Categories
JavaScript Answers

How to Get a Timestamp in JavaScript?

Sometimes, we want to get a timestamp in JavaScript.

In this article, we’ll look at how to get a timestamp in JavaScript.

How to Get a Timestamp in JavaScript?

To get a timestamp in JavaScript, we can use some operators and functions.

For instance, we write

+new Date();
new Date().getTime();
Number(new Date())

to return the timestamp of the current datetime.

We use + before new Date() to get the timestamp of the current datetime in milliseconds.

Likewise, we use the getTime method to return the same thing.

Also, we can call Number with new Date() to return the current datetime’s timestamp

Conclusion

To get a timestamp in JavaScript, we can use some operators and functions.

Categories
JavaScript Answers

How to retrieve the position (X,Y) of an HTML element with JavaScript?

Sometimes, we want to retrieve the position (X,Y) of an HTML element with JavaScript.

In this article, we’ll look at how to retrieve the position (X,Y) of an HTML element with JavaScript.

How to retrieve the position (X,Y) of an HTML element with JavaScript?

To retrieve the position (X,Y) of an HTML element with JavaScript, we use the getBoundingClientRect method.

For instance, we write

const { top, right, bottom, left } = element.getBoundingClientRect();
console.log(top, right, bottom, left);

to call the element.getBoundingClientRect method to return the position of the element.

We get the top, right, bottom, and left position from the object returned.

Conclusion

To retrieve the position (X,Y) of an HTML element with JavaScript, we use the getBoundingClientRect method.

Categories
JavaScript Answers

How to use external .js files with JavaScript?

Sometimes, we want to use external .js files with JavaScript.

In this article, we’ll look at how to use external .js files with JavaScript.

How to use external .js files with JavaScript?

To use external .js files with JavaScript, we use a script tag.

For instance, we write

<script type="text/javascript" src="myscript.js"></script>

to add a script tag that loads myscript.js.

Conclusion

To use external .js files with JavaScript, we use a script tag.

Categories
JavaScript Answers

How to let a user download multiple files when a button is clicked with JavaScript?

Sometimes, we want to let a user download multiple files when a button is clicked with JavaScript.

In this article, we’ll look at how to let a user download multiple files when a button is clicked with JavaScript.

How to let a user download multiple files when a button is clicked with JavaScript?

To let a user download multiple files when a button is clicked with JavaScript, we create a link to the file to download and then click it.

For instance, we write

const urls = [
  "http://example.com/file1",
  "http://example.com/file2",
  "http://example.com/file3",
];

const download = async (urls) => {
  for (const url of urls) {
    const a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("download", "");
    a.setAttribute("target", "_blank");
    a.click();
    await new Promise((resolve) => setTimeout(resolve, 1000));
  }
};

to loop through the url in urls in the download function with a for-of loop.

In it, we create a link with createElement.

Then we set the href attribute to the file url with setAttribute.

Next we call click to click on the link to start the download.

Then we use await on the promise to pause the function for 1 second with setTimeout.

Conclusion

To let a user download multiple files when a button is clicked with JavaScript, we create a link to the file to download and then click it.

Categories
JavaScript Answers

How to push to multidimensional array with JavaScript?

Sometimes, we want to push to multidimensional array with JavaScript.

In this article, we’ll look at how to push to multidimensional array with JavaScript.

How to push to multidimensional array with JavaScript?

To push to multidimensional array with JavaScript, we call push with the array.

For instance, we write

const valuesToPush = Array();
valuesToPush[0] = productID;
valuesToPush[1] = itemColorTitle;
valuesToPush[2] = itemColorPath;

cookieValues.push(valuesToPush);

to create the valuesToPush array with some values.

Then we call push on the cookieValues array to append the valuesToPush array with push.

Conclusion

To push to multidimensional array with JavaScript, we call push with the array.