JS ブラウザタイプを検証

  • 作成者:ayuan0625
  • 編集回数:7次
  • 最終更新:ayuan0625 于 2020-12-17
  • I. Description

    Sometimes IE browsers cannot display some pages, so we need to define different pages for different browser types. How to distinguish the browsers?

    We can make the judgement by the user-agent header of the browser, and then use the pop-up box to display the browser type, the effect is as follows:

    1606357813109927.gif

    II. Example

    Create a new template, insert a button in cellA1, name it check browser, and add a Click event:

    1606357813434047.png

    function myBrowser(){
        var userAgent = navigator.userAgent; //Get the userAgent string of the browser
        var isOpera = userAgent.indexOf("Opera") > -1;
        if (isOpera) {
            return "Opera"
        }; //Determine whether it is Opera browser
        if (userAgent.indexOf("Firefox") > -1) {
            return "FF";
        } //Determine whether it is the Firefox browser
        if (userAgent.indexOf("Chrome") > -1){
      return "Chrome";
     }
        if (userAgent.indexOf("Safari") > -1) {
            return "Safari";
        } //Determine whether it is Safari browser
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            return "IE";
        }; //Determine whether it is IE browser
    }
    //The following is to call the above function
    var mb = myBrowser();
    if ("IE" == mb) {
        alert("I am IE");
    }
    if ("FF" == mb) {
        alert("I am Firefox");
    }
    if ("Chrome" == mb) {
        alert("I am Chrome");
    }
    if ("Opera" == mb) {
        alert("I am Opera");
    }
    if ("Safari" == mb) {
        alert("I am Safari");
    }

    Save the template, click Data Entry Preview, and the effect is as shown in the beginning.

    III. Completed template

    Attachment List


    Theme: FineReport カスタム開発
    既に最初
    既に最後
    • Helpful
    • Not helpful
    • Only read