2016-06-08 60 views
0

我创建了一个函数来获取用户的IP地址。然后我试图给用户分开的AD。所以我使用if条件并尝试使用函数的值(getIP)来执行if条件。但我不是成功的。主要问题是我无法在函数外部使用ip变量。任何人都可以帮助我。如何在函数外使用变量?

<script type="application/javascript"> 
    function getIP(json) { 
    ip=json.ip; //Here I got the user IP Address 
    } 
    getIP(json); 
    var KivaHan="221.120.101.58" 
    var Office="221.120.99.186" 
    if(ip==KivaHan){ 
    document.write("Kiva Han IP Address: " + ip); 
    }else{ 
    document.write("Office IP Address: " + ip); 
    } 
</script> 
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script> 

回答

0

像这样改变它。

  function getIP(json) { 
      return json.ip; 
      } 
      var ip = getIP(json); 
0

如果您getIP功能同步,从它返回的IP地址:

function getIP() { 
    var ip; // Don't forget to declare this 

    // ...get the IP address into `ip` 
    return ip; 
} 

使用

var ip = getIP(); 
if (ip) { 
    // Your code uses `ip` here 
} else { 
    // Failed to get it, handle that here 
} 

如果您getIP功能异步,你有两个选项:

有它接受一个回调,并调用与IP回调:

function getIP(callback) { 
    var ip; // Don't forget to declare this 

    // ...get the IP address into `ip`, maybe have it set to null on error 
    callback(ip); 
} 

用法:

getIP(function(ip) { 
    if (ip) { 
     // Your code uses `ip` here 
    } else { 
     // Failed to get it, handle that here 
    } 
}); 

或者使用承诺书(ES2015,但也有填充工具不够好于库JavaScript引擎是不这样做本身):

function getIP(callback) { 
    var ip; // Don't forget to declare this 
    return new Promise(function(resolve, reject) { 
     // ...get the IP address into `ip` 
     if (/* we got the IP */) { 
      resolve(ip); 
     } else { 
      // it failed: 
      reject(); 
     } 
    }); 
} 

用法:

getIP() 
    .then(function(ip) { 
     // Your code uses `ip` here; perhaps ip == null means failure 
    }) 
    .catch(function() { 
     // Failed to get it, handle that here 
    }); 
0

只有javascript创建一个新的作用域的地方是函数。无论何时你看到函数关键字,它都可以安全地假设将创建一个新的作用域。你在这里遇到的问题是你正在定义一个函数内的ip,并试图在逻辑上不可能的情况下访问它。但是,您可以创建一个全局变量,并可以在程序中的任何地方访问它。但它的缺点是,它会混乱你的全球命名空间,如果你不走运,你可能会面临难以调试的问题。

您可以使用IIFE(立即调用函数表达式),它是javascript中最常用的设计模式之一。

(function(){ 

    var ip = ""; //Define ip variable here. 
    function getIP(json) { 
      ip=json.ip; //Here I got the user IP Address 
      } 
      getIP(json); 
      var KivaHan="221.120.101.58" 
      var Office="221.120.99.186" 
       if(ip==KivaHan){ 
        document.write("Kiva Han IP Address: " + ip); 

       } 

       else{ 

        document.write("Office IP Address: " + ip); 
       } 

})(); 

希望这有些帮助。

快乐学习:)

0

我曾尝试与大多数解决方案,但没有得到我想要真正的输出。也许有getIP(json)函数和我从哪里获取用户IP地址的函数的网站存在问题。我认为这个功能的价值不能超出功能的一面。所以我用另一个网站来获取用户IP。

现在我给出我的需求的替代解决方案。 在这个主题中,我想要一个用户的IP地址,我在该功能中获得。但是我无法从这个功能中找出这个功能,我不能在这个页面上使用更多的时间。

因此,我使用下面给出的解决方案来填补我的需求..

<head> 
<script type="text/javascript" src="https://l2.io/ip.js?var=userip"> </script><!-- To get IP Address of the user --> 
<script type="text/javascript"><!-- Here variables are decleared.. --> 
    var userip; 
    var KivaHan="221.120.101.58" 
    var Office="221.120.99.186" 

</script> 
</head> 
<!-- Javascript for tracking and comparing the user IP--> 

<!-- Below code is for implementation--> 

<div class="main-bottom"> 
     <div class="ad"> 
     <script type="text/javascript"> 
      if (userip==KivaHan){ 

       document.write("Welcome"); 
      } 
      else 
      { 
       document.write("Coming Soon"); 
      } 
     </script> 
     </div> 
     <div class="ad"> 
     <script type="text/javascript"> 
      if (userip==KivaHan){ 

       document.write("Welcome");    

      } 
      else 
      { 
       document.write("Coming Soon"); 
      } 
     </script> 
     </div> 
     <div class="ad"> 
     <script type="text/javascript"> 
      if (userip==KivaHan){ 

       document.write("Welcome"); 

      } 
      else 
      { 
       document.write("Coming Soon"); 
      } 
     </script> 
     </div> 

    </div>