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.

Categories
JavaScript Answers

How to set the value of a input hidden field through JavaScript?

To set the value of a input hidden field through JavaScript, we set the input’s value property.

For instance, we write

document.getElementById("checkyear").value = "1";

to select the input with getElementById.

Then we set its value property to the input value.

Categories
JavaScript Answers

How to save output to variable as an object with JavaScript Fetch API?

Sometimes, we want to save output to variable as an object with JavaScript Fetch API.

In this article, we’ll look at how to save output to variable as an object with JavaScript Fetch API.

How to save output to variable as an object with JavaScript Fetch API?

To save output to variable as an object with JavaScript Fetch API, we call res.json.

For instance, we write

const foo = async () => {
  const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
  const obj = await res.json();
  console.log(obj);
};

foo();

to call fetch to make a get request to https://jsonplaceholder.typicode.com/posts/1.

Then we get the promise with the response body with res.json.

And we get response body with await.

Conclusion

To save output to variable as an object with JavaScript Fetch API, we call res.json.