# Detect if on AnkiMobile using javascript

**URL:** <https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678>\
**Category:** Help\
**Created:** [July 2, 2024, 6:55am UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678 "2024-07-02T06:55:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidereal](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/sidereal/32/25708_2.png) [@sidereal](https://forums.ankiweb.net/u/sidereal)\
**Post date:** [July 2, 2024, 6:55am UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678/1 "2024-07-02T06:55:03Z")

</div>

I am making a card deck, and I can detect whether the client is on the desktop version of Anki or AnkiDroid. If they are not on those platforms, I can deduce that the cards are on either AnkiWeb or AnkiMobile. How can I distinguish between them with just a javascript function returning a boolean? I know that iOS can be detected with javascript, but people use AnkiWeb on Safari or Chrome on Apple devices; I am looking for some detectable feature that is specific to the app, so the help of a developer would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![congbosun](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@congbosun](https://forums.ankiweb.net/u/congbosun)\
**Post date:** [July 2, 2024, 3:18pm UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678/2 "2024-07-02T15:18:35Z")

</div>

I think there will be a class `mobile` in the `<html>` tag.  
You can use js to check it.

Also there is an “Add Mobile Class” option in card template preview feature.

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [July 3, 2024, 3:48pm UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678/3 "2024-07-03T15:48:53Z")

</div>

Please see [Styling & HTML - Anki Manual](https://docs.ankiweb.net/templates/styling.html#platform-specific-css)

---

<div class="post-metadata">

**Author:** ![sidereal](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/sidereal/32/25708_2.png) [@sidereal](https://forums.ankiweb.net/u/sidereal)\
**Post date:** [July 3, 2024, 9:47pm UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678/4 "2024-07-03T21:47:32Z")

</div>

Thank you, both. I ended up using `document.documentElement.classList.contains('')`. The classes `iphone` and `ipad` are specific to AnkiMobile, and `mobile` works for either AnkiDroid or AnkiMobile.

---

<div class="post-metadata">

**Author:** ![huandney](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/huandney/32/18511_2.png) [@huandney](https://forums.ankiweb.net/u/huandney)\
**Post date:** [July 12, 2024, 5:41pm UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678/5 "2024-07-12T17:41:20Z")

</div>

I created a function exactly for this:

```auto
// Detects where the script is being executed
function detectAnkiPlatform() {
    if (window.anki && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.cb !== undefined) {
        return "AnkiMobile";
    } else if (document.querySelector("span.align-middle") && document.querySelector("span.align-middle").textContent.includes("AnkiWeb")) {
        return "AnkiWeb";
    } else if (typeof AnkiDroidJS !== "undefined") {
        return "AnkiDroid";
    } else if (typeof pycmd !== "undefined") {
        return "AnkiDesktop";
    } else {
        return "Unknown";
    }
}

```

If you want to see the complete model [here](https://github.com/huandney/Anki-Cloze-Interactive/blob/main/card/front-template.html).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [August 11, 2024, 5:41pm UTC](https://forums.ankiweb.net/t/detect-if-on-ankimobile-using-javascript/46678/6 "2024-08-11T17:41:41Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
