js gets the operating system and screen resolution

Hi, hello, long time no see, I'm here again. Someone asked a question like this today, how to get the resolution of the screen and the operating system. Because the parameters he needs to pass in the project interface include these two. I searched and sorted it out.

To obtain the operating system, bom has an object called navigator , we open a web page, click F12, then enter navigator in the console , and press Enter. We can see an object with many values ​​in it. As shown below

There are many attributes in it, we can find out which ones are related to the operating system. There is a platform: win32 , and a userAgent , make full use of these two, not much to say, directly on the code:

        function getOperatingSystem() {
            try {
                //检测当前操作系统
                if (/(android|Android|ANDROID)/i.test(navigator.userAgent)) {
                    //这是Android平台下浏览器
                    return "Android";
                }
                if (/(iPhone|iPad|iPod|iOS|iphone|ipad|ipod|ios)/i.test(navigator.userAgent)) {
                    //这是iOS平台下浏览器
                    return "ios"
                }
                if (/win|windows|Win|Windows/i.test(navigator.userAgent)) {
                    //这是Linux平台下浏览器
                    return "windows"
                }
                if (/Linux/i.test(navigator.platform)) {
                    //这是Linux操作系统平台
                    return "linux"
                }
            } catch (e) {
                //可以看看e是什么玩意
                console.log(e);
                //返回未知
                return "Unknow";
            }
        }

The regex is found on the Internet, because the specific writing methods of each item have to be checked out one by one, so that it is rigorous. 

Then call this method, you can call (onload) when mounted , and you will know the operating system.

The second is the resolution of the screen. There are many similar ones, so don't confuse them. A web link is attached, you can click to see how to get similar

JS gets browser information and screen resolution

Looking up the table, we can see that what we want to use is window.screen.height and window.screen.width .

Okay, I'll share it here, see you next time

Supongo que te gusta

Origin blog.csdn.net/weixin_68067009/article/details/125766380
Recomendado
Clasificación