Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a function defined in jQuery.ready available globally?

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).

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 gives me getList is not defined which makes sense, because its not. Am I able to 'globally' declare this function?

like image 408
wesbos Avatar asked Feb 08 '10 16:02

wesbos


People also ask

How do you call a function in document ready jQuery?

jQuery Document Ready Example ready(function() { //DOM manipulation code }); You call jQuery's $ function, passing to it the document object. The $ function returns an enhanced version of the document object. This enhanced object has a ready() function you can call, to which you pass a JavaScript function.

How do you define a global function in JavaScript?

Open JS console, write var x = function() {console. log('x')} and then try to call window. x() . In browsers, window is global scope, therefore the function is global.

What is the ready function in jQuery?

jQuery ready() MethodThe ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

Can we define function in document ready?

Your function is defined within the scope of the $(document). ready() callback and cannot be seen from the outside. Either define the function outside the $(document). ready() scope of call it only from within.


2 Answers

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;    };   

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

like image 105
Andy E Avatar answered Sep 20 '22 13:09

Andy E


Yet another option is to hang the function off the jQuery object itself. That way you avoid polluting the global name space any further:

jQuery.getlist = function getlist(url, gkey) {   // ... } 

Then you can get at it with "$.getlist(url, key)"

like image 38
Pointy Avatar answered Sep 20 '22 13:09

Pointy