0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ブラウザによるuserAgentでの表示変更方法

Last updated at Posted at 2021-10-05

<html>
    <head>
    </head>
    <body>
    <script>

      var isBrowser = navigator.userAgent.indexOf("edge") > 0 || 
                      navigator.userAgent.indexOf("chrome") > 0 || 
                      navigator.userAgent.indexOf("safari") > 0 || 
                      navigator.userAgent.indexOf("firefox") > 0;

        if(isBrowser) {
          // 対応ブラウザ外
          console.log('対応ブラウザではありません。');
        } else {
          // 対応ブラウザ
          console.log('対応ブラウザです。');
        };


      var isBrowser2 = navigator.userAgent.indexOf("edge") < 0 ||
                       navigator.userAgent.indexOf("chrome") < 0 || 
                       navigator.userAgent.indexOf("safari") < 0 || 
                       navigator.userAgent.indexOf("firefox") < 0;

        if(isBrowser2) {
          // 対応ブラウザ
          console.log('対応ブラウザです。');
        } else {
          // 対応ブラウザ外 
         console.log('対応ブラウザではありません。');
        };

      var isNotBrowser = navigator.userAgent.indexOf("edge") > 0 || 
                         navigator.userAgent.indexOf("chrome") > 0 || 
                         navigator.userAgent.indexOf("safari") > 0 || 
                         navigator.userAgent.indexOf("firefox") > 0;
        if(!(isNotBrowser)) {
          // 対応ブラウザ
          console.log('対応ブラウザです。');
        } else {
          // 対応ブラウザ外
          console.log('対応ブラウザではありません。');
        };

      var browserCheck = window.navigator.userAgent.toLowerCase();
        if(browserCheck.indexOf('msie') > 0 || browserCheck.indexOf('trident') > 0) {
          console.log('このブラウザはInternet Explorerです。');
        } else if(browserCheck.indexOf('edge') > 0) {
          console.log('このブラウザはEdgeです。');
        } else if(browserCheck.indexOf('chrome') > 0) {
          console.log('このブラウザはGoogle Chromeです。');
        } else if(browserCheck.indexOf('safari') > 0) {
          console.log('このブラウザはSafariです。');
        } else if(browserCheck.indexOf('firefox') > 0) {
          console.log('このブラウザはFireFoxです。');
        } else if(browserCheck.indexOf('opera') > 0) {
          console.log('このブラウザはOperaです。');
        } else {
          console.log('このブラウザは何かわかりません。');
        }

      var terminalCheck = navigator.userAgent.indexOf("iPhone") > 0 || 
                          navigator.userAgent.indexOf("iPad") > 0 || 
                          navigator.userAgent.indexOf("Android") > 0;
    if(terminalCheck) {
		  // スマホ タブレット
          console.log('スマホです。')
        } else {
		  // PC
          console.log('デスクトップです。')
	    }
    </script>
    </body>
</html>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?