js实现内外网判断

        function ping(ip) {
            var img = new Image();
            var start = new Date().getTime();
            var flag = false;
            var isCloseWifi = true;
            var hasFinish = false;
            img.onload = function () {
                if (!hasFinish) {
                    flag = true;
                    hasFinish = true;
                    img.src = 'X:\\';
                    console.log('Ping ' + ip + ' success. ');
                }
            };


            img.onerror = function () {
                if (!hasFinish) {
                    if (!isCloseWifi) {
                        flag = true;
                        img.src = 'X:\\';
                        console.log('Ping ' + ip + ' success. ');
                        createNewFirst();
                    } else {
                        console.log('network is not working!');
                    }
                    hasFinish = true;
                }
            };


            setTimeout(function () {
                isCloseWifi = false;
                console.log('network is working, start ping...');
            }, 2);


            img.src = 'http://' + ip + '/';
            var timer = setTimeout(function () {
                if (!flag) {
                    hasFinish = true;
                    img.src = 'X://';
                    flag = false;
                    console.log('Ping ' + ip + ' fail. ');
                    createNewSecond();
                }
            }, 1500);
        }
        ping('192.1.94.108');
        //ping('wms.cccbmc.com');
//ping('www.baidu.com');


        function createNewFirst() {
            var if_w = $("body").width();
            var if_h = $(window).height();
            $("<iframe width='" + if_w + "' height='" + if_h + "' id='Frame1'></iframe>").prependTo('body');
            $("#Frame1").attr("src", "https://www.baidu.com/s?tn=monline_3_dg&wd=%E9%A9%AC%E4%BA%91&usm=3&ie=utf-8&rsv_cq=%E6%9D%8E%E5%BD%A6%E5%AE%8F&rsv_dl=0_right_recommends_merge_20826&euri=d722f56c84d64cd78a2e6ee30e3b2cef");
        }


        function createNewSecond() {
            var if_w = $("body").width();
            var if_h = $(window).height();
            $("<iframe width='" + if_w + "' height='" + if_h + "' id='Frame1'></iframe>").prependTo('body');
            $("#Frame1").attr("src", "https://www.baidu.com/s?tn=monline_3_dg&wd=%E6%9D%8E%E5%BD%A6%E5%AE%8F&usm=5&ie=utf-8&rsv_cq=%E7%99%BE%E5%BA%A6&rsv_dl=0_right_recommends_merge_20826&euri=b1f02a70c8e5407bbee068f493457e51");
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值