Categories
JavaScript Answers

How to detect Chrome and Safari browser with JavaScript?

Sometimes, we want to detect Chrome and Safari browser with JavaScript.

In this article, we’ll look at how to detect Chrome and Safari browser with JavaScript.

How to detect Chrome and Safari browser with JavaScript?

To detect Chrome and Safari browser with JavaScript, we use the user agent and vendor strings.

For instance, we write

const isChrome =
  /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

const isSafari =
  /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);

if (isChrome) {
  alert("You are using Chrome!");
}

if (isSafari) {
  alert("You are using Safari!");
}

to check for Chrome with /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor).

And we check for Safari with /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor).

userAgent is the user agent string.

And vendor is the vendor string.

Conclusion

To detect Chrome and Safari browser with JavaScript, we use the user agent and vendor strings.

Categories
JavaScript Answers

How to hide blinking cursor in input text with CSS?

Sometimes, we want to hide blinking cursor in input text with CSS.

In this article, we’ll look at how to hide blinking cursor in input text with CSS.

How to hide blinking cursor in input text with CSS?

To hide blinking cursor in input text with CSS, we set the caret-color property.

For instance, we write

input {
  caret-color: transparent;
}

to set the caret-color to transparent to make the blinking cursor disappear from the input.

Conclusion

To hide blinking cursor in input text with CSS, we set the caret-color property.

Categories
JavaScript Answers

How to add CSS animation on click with JavaScript?

Sometimes, we want to add CSS animation on click with JavaScript.

In this article, we’ll look at how to add CSS animation on click with JavaScript.

How to add CSS animation on click with JavaScript?

To add CSS animation on click with JavaScript, we add animation effects for a class.

For instance, we write

.classname {
  -webkit-animation-name: cssAnimation;
  -webkit-animation-duration: 3s;
  -webkit-animation-iteration-count: 1;
  -webkit-animation-timing-function: ease;
  -webkit-animation-fill-mode: forwards;
}

@-webkit-keyframes cssAnimation {
  from {
    -webkit-transform: rotate(0deg) scale(1) skew(0deg) translate(100px);
  }
  to {
    -webkit-transform: rotate(0deg) scale(2) skew(0deg) translate(100px);
  }
}

to add animation to the classname class.

In it, we add the keyframes for the animation.

Then we write

const ani = () => {
  document.getElementById("img").className = "classname";
};

to define the ani function that selects the element to animate with getElementById.

And we set its className to 'classname' to apply the animation effects that’s applied for the class.

Conclusion

To add CSS animation on click with JavaScript, we add animation effects for a class.

Categories
JavaScript Answers

How to make a checkbox read only with HTML and JavaScript?

Sometimes, we want to make a checkbox read only with HTML and JavaScript.

In this article, we’ll look at how to make a checkbox read only with HTML and JavaScript.

How to make a checkbox read only with HTML and JavaScript?

To make a checkbox read only with HTML and JavaScript, we return false when we click on the checkbox.

For instance, we write

<input type="checkbox" onclick="return false" />

to set onclick to return false` to stop clicks from modifying the checkbox’s state.

Conclusion

To make a checkbox read only with HTML and JavaScript, we return false when we click on the checkbox.

Categories
JavaScript Answers

How to load HTML file contents to div with JavaScript?

Sometimes, we want to load HTML file contents to div with JavaScript.

In this article, we’ll look at how to load HTML file contents to div with JavaScript.

How to load HTML file contents to div with JavaScript?

To load HTML file contents to div with JavaScript, we use fetch.

For instance, we write

const loadHtml = async () => {
  const response = await fetch("page.html");
  const text = await response.text();
  document.getElementById("elementID").insertAdjacentText("beforeend", text);
};

loadHtml();

to define the loadHtml function.

In it, we call fetch to get the content of page.html.

We get it as a string with response.text.

And then we call insertAdjacentText to insert the HTMLK into the element we selected with getElementById.

Conclusion

To load HTML file contents to div with JavaScript, we use fetch.