判断内网还是外网

126 阅读1分钟

场景:公司官网上某模块,公司内部网络可以访问,外部网络禁止访问。

const checkWebSiteOnline = (config) => {
        var img = document.createElement('img')
        img.onload = function () {
            if (typeof config.success === 'function') config.success(config.url)
        }
        img.onerror = function () {
            if (typeof config.error === 'function') config.error(config.url)
        }
        img.src = config.url
        img.remove()
    }
    checkWebSiteOnline({
        url: "http://192.168.1.5:3000/src/assets/logo.png",
        success: (url) => {
            alert("网络为内网")
            console.log('success====>>', url)
        },
        error: (error) => {
            alert("网络为外网")
            console.log('error====>>', error)
        }
    })

其中,“http://192.168.1.5:3000/src/assets/logo.png” 为放在公司内部服务器上的一张图片,

若能访问则为内网,不能访问则为外网。