How to implement touch functionality on a Windows PC using jQuery / Javascript?

I have implemented touch functionality on my website using this code, and it works great for iPad / iPhone.

// registering touch events
function initTouchEvents() {

    // if a device is a touch device
    if (window.Touch) {
        document.addEventListener("touchstart", touchHandler, true);
        document.addEventListener("touchmove", touchHandler, true);
        document.addEventListener("touchend", touchHandler, true);
        document.addEventListener("touchcancel", touchHandler, true);
    }
}

// making items sortable in touch screen devices
function touchHandler(event) {

    var touches = event.changedTouches,
                    first = touches[0],
                    type = "";

    switch (event.type) {
        case "touchstart": type = "mousedown"; break;
        case "touchmove": type = "mousemove"; break;
        case "touchend": type = "mouseup"; break;
        default: return;
    }
    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY,
                                  first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);

    first.target.dispatchEvent(simulatedEvent);

    if (event.type == "touchmove") {

        event.preventDefault();
    }
}

But when I tested my site on a PC with a touch screen, it did not start, and I found out that it window.Touchworks only for iPhone / iPad. And I also tried various other events, such as typeof(window.ontouchstart != 'undefined')and ('ontouchstart' in window || typeof TouchEvent != "undefined"), but did not find that it has a touch screen and does not register an event for a touch movement.

What I am asking for here is an event in javascript that can detect all touch devices (IOS / Android / Windows / OSX) and trigger event logging.

+3
source share
2 answers

, " " . .

CTRL + SHIFT + I F12, . " ". , .

+1

All Articles