如何使用函数外的变量?

时间:2016-06-08 06:32:07

标签: javascript

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

#my-navbar-collapse {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    width: 280px; /*example + never use min-width with this solution */
    height: 100%;
}

4 个答案:

答案 0 :(得分:0)

像这样改变它。

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

答案 1 :(得分: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
    }
});

或者使用Promise(ES2015,但是有些libs可以在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
   });

答案 2 :(得分:0)

只有javascript创建新范围的地方才是功能。每当看到function关键字时,可以安全地假设将创建一个新的范围。你在这里面临的问题是你在一个函数中定义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);
                }

})();

希望这会有所帮助。

快乐学习:)

答案 3 :(得分: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>