html 检测是否支持协议处理程序(通过带有适当Content-Type标头的rawgit提供)http://rawgit.com/masdeseiscaracteres/d92bb8

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 检测是否支持协议处理程序(通过带有适当Content-Type标头的rawgit提供)http://rawgit.com/masdeseiscaracteres/d92bb8相关的知识,希望对你有一定的参考价值。

<html>
<head>
    <title>Detect Custom Protocol</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
</head>
<body>
<input id="protocol" value="" placeholder="custom protocol"/>
<button id="launch">Launch</button>
<!-- Mozilla Only -->
<iframe id="hiddenIframe" src="about:blank" style="display:none"></iframe>
<!-- IE Case 1 -->
<a id="hiddenLink" style="display:none;" href="#">custom protocol</a>
<script>

    //Default State
    var isSupported = false;

    //Helper Methods
    function getProtocol(){
        return $('#protocol').val();
    }

    function getUrl(){
        return getProtocol()+"://"+"rajeshsegu.com";
    }

    function result(){
        alert(getProtocol() + " supported => " + isSupported);
    }

    //Handle Click on Launch button
    $('#launch').click(function(){
        if($.browser.mozilla){
            launchMozilla();
        }else if($.browser.chrome){
            launchChrome();
        }else if($.browser.msie){
            launchIE();
        }
    });

    //Handle IE
    function launchIE(){

        var url = getUrl(),
            aLink = $('#hiddenLink')[0];

        isSupported = false;
        aLink.href = url;

        //Case 1: protcolLong
        console.log("Case 1");
        if(navigator.appName=="Microsoft Internet Explorer"
                && aLink.protocolLong=="Unknown Protocol"){
            isSupported = false;
            result();
            return;
        }
 
        //IE10+
        if(navigator.msLaunchUri){
            navigator.msLaunchUri(url, 
                   function(){ isSupported = true; result(); }, //success
                   function(){ isSupported=false; result();  }  //failure 
            );
            return;
        }

        //Case2: Open New Window, set iframe src, and access the location.href
        console.log("Case 2");
        var myWindow = window.open('','','width=0,height=0');
        myWindow.document.write("<iframe src='"+ url + "></iframe>");
        setTimeout(function(){
            try{
                myWindow.location.href;
                isSupported = true;
            }catch(e){
                //Handle Exception
            }

            if(isSupported){
                myWindow.setTimeout('window.close()', 100);
            }else{
                myWindow.close();
            }
            result();
        }, 100)

    };

    //Handle Firefox
    function launchMozilla(){

        var url = getUrl(),
            iFrame = $('#hiddenIframe')[0];

        isSupported = false;

        //Set iframe.src and handle exception
        try{
            iFrame.contentWindow.location.href = url;
            isSupported = true;
            result();
        }catch(e){
            //FireFox
            if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL"){
                isSupported = false;
                result();
            }
        }
    }

    //Handle Chrome
    function launchChrome(){

        var url = getUrl(),
            protcolEl = $('#protocol')[0];

        isSupported = false;


        protcolEl.focus();
        protcolEl.onblur = function(){
            isSupported = true;
            console.log("Text Field onblur called");
        };

        //will trigger onblur
        location.href = url;
        
        //Note: timeout could vary as per the browser version, have a higher value
        setTimeout(function(){
            protcolEl.onblur = null;
            result()
        }, 500);   

    }
</script>
</body>
</html>

以上是关于html 检测是否支持协议处理程序(通过带有适当Content-Type标头的rawgit提供)http://rawgit.com/masdeseiscaracteres/d92bb8的主要内容,如果未能解决你的问题,请参考以下文章

C 错误处理

C ++如何使用处理程序检测窗口状态是不是在全屏?

如何适当地重用 mocha 测试的 describe 块?

25.错误处理

RTP协议之Header结构解析

如何检测浏览器的协议处理程序?