如何在全局提供jQuery.ready中定义的函数? [英] How can I make a function defined in jQuery.ready available globally?

查看:112
本文介绍了如何在全局提供jQuery.ready中定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能可以从网址中删除youtube id。然后我想每页10次使用这个函数(在wordpress循环中)。

I have a function that strips the youtube id off a url. I then want to use this function 10 time per page (in the wordpress loop).

当我在函数脚本标签中输入url时,该函数效果很好,但是当我在循环中开始一组新的脚本标记时,它不起作用。

The function works great when I feed it the url within my function script tags, but when I start a new set of script tags within the loop, it does not work.

我需要知道如何在不首先声明它的情况下使用我的函数。

I need to know how I can use my function without declaring it all first.

所以这是我在标题中的代码:

So this is the code I have in the header:

 <script type="text/javascript"> 
$(document).ready(function() {
var getList = function(url, gkey){
        var returned = null;
        if (url.indexOf("?") != -1){
          var list = url.split("?")[1].split("&"),
                  gets = [];

          for (var ind in list){
            var kv = list[ind].split("=");
            if (kv.length>0)
                gets[kv[0]] = kv[1];
        }

        returned = gets;

        if (typeof gkey != "undefined")
            if (typeof gets[gkey] != "undefined")
                returned = gets[gkey];

        }

            return returned;

    };


        // THIS WORKS

    alert(getList('http://www.youtube.com/watch?v=dm4J5dAUnR4', "v"));


      });

但是当我尝试在页面上的其他地方使用它时,它不起作用。

But when I try use this somewhere else on the page, it doesnt work.

 <script type="text/javascript"> 

      $(document).ready(function() {
              alert(getList('http://www.youtube.com/watch?v=dm4J5dAUnR4', "v"));
      };
      </script>

Firebug给了我 getList未定义这是有道理的,因为它不是。我能够'全局'声明这个函数吗?

Firebug gives me getList is not defined which makes sense, because its not. Am I able to 'globally' declare this function?

推荐答案

您有两个选项,将其添加到窗口对象以使其成为全局:

You have two options, add it to the window object to make it global:

window.getList = function(url, gkey){ 
    // etc...
}

或将其从文档就绪事件处理程序中移动到全局范围内:

or move it from inside the document ready event handler into the global scope:

$(document).ready(function() {  
    alert(getList('http://www.youtube.com/watch?v=dm4J5dAUnR4', "v"));
});  
var getList = function(url, gkey){  

    var returned = null;  
    if (url.indexOf("?") != -1){  
      var list = url.split("?")[1].split("&"),  
              gets = [];  

      for (var ind in list){  
        var kv = list[ind].split("=");  
        if (kv.length>0)  
            gets[kv[0]] = kv[1];  
    }  

    returned = gets;  

    if (typeof gkey != "undefined")  
        if (typeof gets[gkey] != "undefined")  
            returned = gets[gkey];  

    }  

        return returned;  

};  

您可能还想阅读这个问题关于使用 var functionName = function(){} vs function functionName(){} ,以及这篇文章关于可变范围。

You might also want to read this question about using var functionName = function () {} vs function functionName() {}, and this article about variable scope.

这篇关于如何在全局提供jQuery.ready中定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆