I have a function that removes youtube id from url. Then I want to use this function 10 times per page (in the wordpress chain).
This function works fine when I feed it with the URL in a script function tag, but when I run a new set of script tags in a loop, it does not work.
I need to know how I can use my function without declaring it first.
So this is the code that 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; };
But when I try to use it somewhere else on the page, it does not work.
<script type="text/javascript"> $(document).ready(function() { alert(getList('http://www.youtube.com/watch?v=dm4J5dAUnR4', "v")); }; </script>
Firebug gives me getList not defined , which makes sense because it doesn't exist. Can I declare this function globally?
javascript jquery
wesbos Feb 08 '10 at 16:58 2010-02-08 16:58
source share