-1

How can I check if the user has a cursor to hover on things, preferably using javacscript? (Using bootstrap too)

if (hasCursor()) {
  doCode();
}

I tried just checking if it is a touch device, but my laptop is a 2-in-1, so I cant test it.

Pixlmoon
  • 3
  • 2
  • I guess it will help you out: https://stackoverflow.com/questions/7838680/detecting-that-the-browser-has-no-mouse-and-is-touch-only/52854585#answer-52854585 – RK_oo7 Mar 25 '23 at 07:26

1 Answers1

0

Use the official interface

You are testing for the ability to hover, which is currently in practice synonymous with having a cursor, but might not be in future. So use the any-hover query that tests whether the user has any method of hovering.

if(window.matchMedia("(any-hover: none)").matches) {
    doCode();
}
ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26