Categories
JavaScript Answers

How to pass variable through JavaScript from one html page to another page?

Sometimes, we want to pass variable through JavaScript from one html page to another page.

In this article, we’ll look at how to pass variable through JavaScript from one html page to another page.

How to pass variable through JavaScript from one html page to another page?

To pass variable through JavaScript from one html page to another page, we can use local storage to store the variable’s value.

Then we can get the value in the other page.

For instance, we write:

localStorage.setItem("name", 'jane');

to store an item with key 'name' and value 'jane' into local storage with localStorage.setItem.

Then on the other page, we can get the value with localStorage.getItem by writing:

localStorage.getItem("name");

which should return 'jane'.

Conclusion

To pass variable through JavaScript from one html page to another page, we can use local storage to store the variable’s value.

Then we can get the value in the other page.

Categories
JavaScript Answers

How to read a file as binary with the HTML5 File API?

Sometimes, we want to read a file as binary with the HTML5 File API.

In this article, we’ll look at how to read a file as binary with the HTML5 File API.

How to read a file as binary with the HTML5 File API?

To read a file as binary with the HTML5 File API, we can use the readAsArrayBuffer method.

For instance, we write:

<input type='file'>

to add a file input element.

Then, we write:

const reader = new FileReader()
reader.onload = () => {
  console.log(reader.result)
}

const input = document.querySelector('input')
input.addEventListener('change', e => {
  const [file] = e.target.files
  reader.readAsArrayBuffer(file)
})

to create a new FileReader instance.

And we set the reader.onload property to a function that logs the reader.result property, which has the read array buffer.

Then we select the input with document.querySelector.

And then we call input.addEventListener with 'change' to add a change event listener.

Then in the change listener, we get the file from e.target.files with destructuring.

And then we call reader.readAsArrayBuffer with the file.

Now when we select a file, we should see the array buffer logged.

Conclusion

To read a file as binary with the HTML5 File API, we can use the readAsArrayBuffer method.

Categories
JavaScript Answers

How to use the JavaScript array filter but only take first result?

Sometimes, we want to use the JavaScript array filter but only take first result.

In this article, we’ll look at how to use the JavaScript array filter but only take first result.

How to use the JavaScript array filter but only take first result?

To use the JavaScript array filter but only take first result, we can use the JavaScript array destructuring syntax.

For instance, we write:

const [first] = [7, 5, 3, 2, 1].filter(x => x % 2 === 0)
console.log(first)

to call filter with a callback that checks if the element is even to return an array of even numbers.

Then we use the destructuring syntax to get the first element from the returned array and assign it to first.

Therefore, first is 2 according to the console log.

Conclusion

To use the JavaScript array filter but only take first result, we can use the JavaScript array destructuring syntax.

Categories
JavaScript Answers

How to check if object is a textbox with JavaScript?

Sometimes, we want to check if object is a textbox with JavaScript.

In this article, we’ll look at how to check if object is a textbox with JavaScript.

How to check if object is a textbox with JavaScript?

To check if object is a textbox with JavaScript, we can check if the object is an instance of HTMLInputElement and that its type property is set to 'text'.

For instance, we write:

<input>

to add an input element.

Then we can select it and check if it’s a textbox by writing:

const obj = document.querySelector('input')
const isInputText = obj instanceof HTMLInputElement && obj.type === 'text';
console.log(isInputText)

We select the element with document.querySelector.

Then we use the instanceof operator to check that it’s an instance of HTMLInputElement.

And then we make sure that its type attribute is set to text with obj.type === 'text'.

Therefore the console log should log true.

Conclusion

To check if object is a textbox with JavaScript, we can check if the object is an instance of HTMLInputElement and that its type property is set to 'text'.

Categories
JavaScript Answers

How to scroll to an element without # in the URL with JavaScript?

Sometimes, we want to scroll to an element without # in the URL with JavaScript.

In this article, we’ll look at how to scroll to an element without # in the URL with JavaScript.

How to scroll to an element without # in the URL with JavaScript?

To scroll to an element without # in the URL with JavaScript, we can use the scrollIntoView method.

For instance, we write:

<p id='top'>top</p>

<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>

<button>
  scroll to top
</button>

to add some elements onto the page.

Then we write:

const button = document.querySelector('button')

button.addEventListener('click', () => {
  document.getElementById('top').scrollIntoView(true);
})

We select the button with document.querySelector.

Then we call button.addEventListener to add a click listener to the button.

In the click listener, we select the element with id top with document.getElementById.

Then we call scrollIntoView with true to scroll to the element we selected.

Now when we click on the button, we should scroll back to the top of the page.

Conclusion

To scroll to an element without # in the URL with JavaScript, we can use the scrollIntoView method.