1. 程式人生 > >利用js自動檢測pc端和移動端,js程式碼,需要寫兩個網頁,一個pc,一個移動端

利用js自動檢測pc端和移動端,js程式碼,需要寫兩個網頁,一個pc,一個移動端

假設pc/index.html是pc端的網頁,mobile/index.html是移動端的網頁
在外部設定一個html進行判斷,分別跳轉;

//判斷如果是pc端,自動跳到pc/index.html
//安卓手機自動跳到mobile/index.html
var userAgent = navigator.userAgent.toLowerCase();

        if(userAgent.indexOf('android') == -1 &&
        userAgent.indexOf('iphone') == -1 &&
        userAgent.indexOf('ipad') == -1)

        location.href = 'pc/index.html';
        else
        location.href = 'mobile/index.html';