Categories
JavaScript Answers

How to open window in JavaScript with HTML inserted?

Sometimes, we want to open window in JavaScript with HTML inserted.

In this article, we’ll look at how to open window in JavaScript with HTML inserted.

How to open window in JavaScript with HTML inserted?

To open window in JavaScript with HTML inserted, we can set the innerHTML property of the window’s body element.

For instance, we write

const win = window.open(
  "",
  "Title",
  "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=" +
    (screen.height - 400) +
    ",left=" +
    (screen.width - 840)
);
win.document.body.innerHTML = "HTML";

to call window.open with a string with the dimensions and other settings of the window.

Then we set win.document.body.innerHTML to the HTML string we want to render in the window.

Conclusion

To open window in JavaScript with HTML inserted, we can set the innerHTML property of the window’s body element.

Categories
JavaScript Answers

How to return HTML with fetch() and JavaScript?

Sometimes, we want to return HTML with fetch() and JavaScript.

In this article, we’ll look at how to return HTML with fetch() and JavaScript.

How to return HTML with fetch() and JavaScript?

To return HTML with fetch() and JavaScript, we can use the response text method.

For instance, we write

const fetchMyDocument = async () => {
  try {
    const response = await fetch("/path/to/file.html");
    document.body.innerHTML = await response.text();
  } catch (err) {
    console.log(err);
  }
};

to call fetch to make a GET request to "/path/to/file.html".

Then we get the response from the promise returned by fetch.

Finally, we get the response body HTML from the promise returned by the response.text method with await.

Conclusion

To return HTML with fetch() and JavaScript, we can use the response text method.

Categories
JavaScript Answers

How to copy to clipboard using JavaScript in iOS?

Sometimes, we want to copy to clipboard using JavaScript in iOS.

In this article, we’ll look at how to copy to clipboard using JavaScript in iOS.

How to copy to clipboard using JavaScript in iOS?

To copy to clipboard using JavaScript in iOS, we use the input.setSelectionRange method.

For instance, we write

const copyText = (input) => {
  const isIOSDevice = navigator.userAgent.match(/ipad|iphone/i);

  if (isIOSDevice) {
    input.setSelectionRange(0, input.value.length);
  } else {
    input.select();
  }

  document.execCommand("copy");
};

to define the copyText function.

In it, we check if the function is run on an iOS device by checking the user agent with navigator.userAgent.

We check if 'iPad' or 'iPhone' is in the user agent string.

If it is, we call input.setSelectionRange to select the input’s text.

Otherwise, we use input.select to do the same thing.

Then we call document.execCommand with 'copy' to copy the selected text to the clipboard.

Conclusion

To copy to clipboard using JavaScript in iOS, we use the input.setSelectionRange method.

Categories
JavaScript Answers

How to get column from a two dimensional array with JavaScript?

Sometimes, we want to get column from a two dimensional array with JavaScript.

In this article, we’ll look at how to get column from a two dimensional array with JavaScript.

How to get column from a two dimensional array with JavaScript?

To get column from a two dimensional array with JavaScript, we can use the array map method.

For instance, we write

const arrayColumn = (arr, n) => arr.map((x) => x[n]);

const twoDimensionalArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(arrayColumn(twoDimensionalArray, 0));

to define the arrayColumn function that return an array with the items from column n in the 2D array arr.

We call map with a callback that returns the item at index n of each array in arr.

Then we call arrayColumn with twoDimensionalArray and 0 to get [1, 4, 7].

Conclusion

To get column from a two dimensional array with JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to check for duplicate strings in JavaScript array?

Sometimes, we want to check for duplicate strings in JavaScript array.

In this article, we’ll look at how to check for duplicate strings in JavaScript array.

How to check for duplicate strings in JavaScript array?

To check for duplicate strings in JavaScript array, we can use a set.

For instance, we write

const checkIfDuplicateExists = (arr) => {
  return new Set(arr).size !== arr.length;
};

const arr = ["a", "a", "b", "c"];

console.log(checkIfDuplicateExists(arr));

to define the checkIfDuplicateExists function to check if the arr converted to a set has the same size as the original arr array.

If they have the same size, then there’re no duplicate values in arr since sets can’t have duplicate values but arrays can.

Conclusion

To check for duplicate strings in JavaScript array, we can use a set.