简体   繁体   English

让javascript忽略链接到内部或外部Webadress

[英]let javascript deside wether to link to internal or external webadress

For my new website I'm trying to setup a html with a little javascript which decides whether to link to the external or internal ip. 对于我的新网站,我正在尝试用一个小的javascript设置html,以决定是链接到外部还是内部ip。

I looked up some javascript to request the ip, which I found. 我查了一些JavaScript请求IP,我找到了。

$.getJSON('//api.ipify.org?format=jsonp&callback=?', function(data) {

console.log(JSON.stringify(data, null, 2)); console.log(JSON.stringify(data,null,2)); }); });

which should return this: 应该返回以下内容:

{
  "ip": "116.12.250.1"
}

the point here is that I do not understand how to use this output. 这里的重点是我不明白如何使用此输出。

The first thing I thought of was to write a little javascript using conditions ( https://www.w3schools.com/js/js_if_else.asp ) But I just don't understand how to use this output in this way. 我想到的第一件事是使用条件( https://www.w3schools.com/js/js_if_else.asp )编写一些javascript,但我只是不了解如何以这种方式使用此输出。

The essential idea of my plan is for the website to find out whether the client is on the wan or lan network, using this request. 我的计划的基本思想是让网站使用此请求来确定客户端是处于广域网还是局域网中。 And then decide whether to link the href to an external ip; 然后决定是否将href链接到外部IP; (wan.wan.wan.wan) or internal ip; (wan.wan.wan.wan)或内部ip; (lan.lan.lan.lan) (lan.lan.lan.lan)

so this is what I came up with 这就是我想出的

    if ("wan.wan.wan.wan" == "[the ip the website is hosted on]")
{ "[href variable]" = "lan.lan.lan.lan }

else if ("wan.wan.wan.wan" != "[the ip the website is hosted on]")
{ "[href variable]" = "wan.wan.wan.wan"}

I had a bit of a hard time explaining it, sorry for that. 我对此很难解释,对此感到抱歉。 I'm originally dutch so, forgive me. 我本来是荷兰人,所以请原谅我。

Please ask if my question is unclear in any way!!! 请问我的问题是否仍然不清楚!!!

thanks in advance! 提前致谢!

You can use the request to get the IP address, then save it: 您可以使用请求获取IP地址,然后将其保存:

$.getJSON('//api.ipify.org?format=jsonp&callback=?', function(data) {
    var ip = (JSON.stringify(data, null, 2)); })['ip'];
}

Now that you have the IP address, you can determine if they're on the same network as the server by looking at the IP address, but you can omit the digits following the final . 现在您有了IP地址,可以通过查看IP地址来确定它们是否与服务器位于同一网络上,但是可以省略final后面的数字.

Because if you connect two computers to the same network, one might have the address 10.10.10.10 , and the other 10.10.10.11 , etc. 因为如果将两台计算机连接到同一网络,则一台计算机的地址可能为10.10.10.10 ,另一台计算机的地址为10.10.10.1010.10.10.11

So you check if the first 3 chunks of the visitor's IP matches the IP of your server, which is known: 因此,您检查访问者IP的前3个块是否与您服务器的IP相匹配,即:

$.getJSON('//api.ipify.org?format=jsonp&callback=?', function(data) {
            var ip = (JSON.stringify(data, null, 2))['ip'];

            if (ip.match(/100.100.100.d{0,3}/)) { //REPLACE 100.100.100 WITH YOUR SERVER'S IP
                //user is on the same network
            } else {
                //user is not on the same network
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM