The forum of the forums

Would you like to react to this message? Create an account in a few clicks or log in to continue.
The forum of the forums
4 posters

    Javascript distinguish mobile from desktop

    Fildragon
    Fildragon
    New Member


    Posts : 21
    Reputation : 1
    Language : english

    Solved Javascript distinguish mobile from desktop

    Post by Fildragon Mon 20 Mar - 19:37

    Is there a way within forumotion to distinguish if the user is running the mobile or desktop version or would I have to use the useragent in javascript. I want my script to only run on the desktop version as it breaks the mobile version if it executes there.


    Last edited by Fildragon on Tue 21 Mar - 21:07; edited 1 time in total
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51498
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Javascript distinguish mobile from desktop

    Post by SLGray Mon 20 Mar - 20:45

    So you want this JavaScript to not run while someone is viewing your forum in mobile version?  Does it work fine when you switch to classic version?



    Javascript distinguish mobile from desktop Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    Fildragon
    Fildragon
    New Member


    Posts : 21
    Reputation : 1
    Language : english

    Solved Re: Javascript distinguish mobile from desktop

    Post by Fildragon Mon 20 Mar - 21:21

    www.igdid.com

    yes, it works fine on the forum (though I have disabled it for now). But if the javascript loads on the mobile version it causes issues there. So I would like to adjust my js so that if the mobile version loads it doesnt load this script.
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Javascript distinguish mobile from desktop

    Post by Ange Tuteur Tue 21 Mar - 1:35

    Hey,

    You can check the _userdata object to see if tpl_mobile has been defined. For example :
    Code:
    $(function() {
      if (!_userdata.tpl_mobile) {
        console.log('Desktop');
      }
    });

    Remove the NOT flag "!" if you want the if block to run code for mobile instead. You can also do the standard if ... else statement.
    Code:
    $(function() {
      if (_userdata.tpl_mobile) {
        console.log('Mobile');
      } else {
        console.log('Desktop');
      }
    });
    Fildragon
    Fildragon
    New Member


    Posts : 21
    Reputation : 1
    Language : english

    Solved Re: Javascript distinguish mobile from desktop

    Post by Fildragon Tue 21 Mar - 7:32

    Thankyou so much, you have just my job so much easier. This works great Smile
    Ape
    Ape
    Administrator
    Administrator


    Male Posts : 19324
    Reputation : 2005
    Language : fluent in dork / mumbojumbo & English haha

    Solved Re: Javascript distinguish mobile from desktop

    Post by Ape Tue 21 Mar - 14:40

    @Fildragon is this now solved ?

    If yes then please follow the rules and click the green mark solved button on the first post this will then mark the thread solved and a staff member can close it and more it to the right place.

    A big thank you to @Ange Tuteur for he's help in this topic.



    Javascript distinguish mobile from desktop Left1212Javascript distinguish mobile from desktop Center11Javascript distinguish mobile from desktop Right112
    Javascript distinguish mobile from desktop Ape_b110
    Javascript distinguish mobile from desktop Ape1010
    Fildragon
    Fildragon
    New Member


    Posts : 21
    Reputation : 1
    Language : english

    Solved Re: Javascript distinguish mobile from desktop

    Post by Fildragon Tue 21 Mar - 21:07

    I did mark it as solved
    Ape
    Ape
    Administrator
    Administrator


    Male Posts : 19324
    Reputation : 2005
    Language : fluent in dork / mumbojumbo & English haha

    Solved Re: Javascript distinguish mobile from desktop

    Post by Ape Wed 22 Mar - 0:41

    Problem solved & topic archived.
    Please read our forum rules: ESF General Rules



    Javascript distinguish mobile from desktop Left1212Javascript distinguish mobile from desktop Center11Javascript distinguish mobile from desktop Right112
    Javascript distinguish mobile from desktop Ape_b110
    Javascript distinguish mobile from desktop Ape1010

      Current date/time is Sun 22 Sep - 15:14