Categories
JavaScript Answers

How to change the URL without reloading the page with JavaScript?

Sometimes, we want to change the URL without reloading the page with JavaScript.

In this article, we’ll look at how to change the URL without reloading the page with JavaScript.

How to change the URL without reloading the page with JavaScript?

To change the URL without reloading the page with JavaScript, we can use window.history.pushState.

For instance, we write:

window.history.pushState("state", "title", "/new-url");

to change the URL to /new-url.

The first argument is the state, which can be anything that is serializable.

Then 2nd argument is the title, which is ignored by most browsers.

Conclusion

To change the URL without reloading the page with JavaScript, we can use window.history.pushState.

Categories
JavaScript Answers

How to remove strings in beginning and end with JavaScript?

Sometimes, we want to remove strings in beginning and end with JavaScript.

In this article, we’ll look at how to remove strings in beginning and end with JavaScript.

How to remove strings in beginning and end with JavaScript?

To remove strings in beginning and end with JavaScript, we can use the string trim method.

For instance, we write:

const s = '  foo  '
const trimmed = s.trim()
console.log(trimmed)

to call s.trim to return a string without the whitespaces at the beginning and the end of s.

Therefore, trimmed is 'foo'.

Conclusion

To remove strings in beginning and end with JavaScript, we can use the string trim method.

Categories
JavaScript Answers

How to use string includes() in a JavaScript switch case?

Sometimes, we want to use string includes() in a JavaScript switch case.

In this article, we’ll look at how to use string includes() in a JavaScript switch case.

How to use string includes() in a JavaScript switch case?

To use string includes() in a JavaScript switch case, we should replace switch and case with if statements.

For instance, we write:

const id = "someId";
if (id.includes('product')) {
  //...
} else if (id.includes('user')) {
  //...
}

to call id.includes with the substrings we’re checking and run the code we want according to the checks.

Conclusion

To use string includes() in a JavaScript switch case, we should replace switch and case with if statements.

Categories
JavaScript Answers

How to convert HTML character entities back to regular text using JavaScript?

Sometimes, we want to convert HTML character entities back to regular text using JavaScript

In Hit article, we’ll look at how to convert HTML character entities back to regular text using JavaScript.

How to convert HTML character entities back to regular text using JavaScript?

To convert HTML character entities back to regular text using JavaScript, we can put the HTML character entities into an element.

Then we can get the regular text from the element.

For instance, we write:

const decodeEntities = (s) => {
  const el = document.createElement('p');
  el.innerHTML = s;
  const str = el.textContent
  return str;
}

console.log(decodeEntities('<'))

to define the decodeEntities that takes the s string.

In it, we create a p element with createElement.

Then we set el.innerHTML to s.

And then we return el.textContent which has the regular text converted from s.

As a result, the console log logs '<'.

Conclusion

To convert HTML character entities back to regular text using JavaScript, we can put the HTML character entities into an element.

Then we can get the regular text from the element.

Categories
JavaScript Answers

How to get span value with JavaScript?

Sometimes, we want to get span value with JavaScript.

In this article, we’ll look at how to get span value with JavaScript.

How to get span value with JavaScript?

To get span value with JavaScript, we can use getElementsByTagName.

For instance, we write:

<div id="test">
  <span>1</span>
  <span>2</span>
  <span>3</span>
  <span>4</span>
</div>

to add a div with spans in it.

Then we write:

const div = document.getElementById("test");
const spans = div.getElementsByTagName("span");

for (const s of spans) {
  console.log(s.innerHTML);
}

to select the div with ID test with getElemetById.

Then we call div.getElementsByTagName with 'span' to select the spans.

Next, we loop through the spans with a for-of loop and log the value of the innerHTML property to log the content of the spans.

Therefore,

"1"
"2"
"3"
"4"

is logged.

Conclusion

To get span value with JavaScript, we can use getElementsByTagName.