Why must I define a variable outside a javascript function definition? (jQuery / #anchor question)
<p>I am writing a jQuery function that relies on knowing the current #anchor of the page. I am using the jQuery URL Parser plugin to get the anchor.</p>

<pre><code> $.fn.vtabs = function() {
   alert("Your anchor is "+$.url.attr('anchor'));
 }
</code></pre>

<p>This code gives me the anchor "#nav" endlessly (I use #nav in some of the my links). I can type in "#newanchor" into the browser bar endless times, and click url's to this page that use different anchors, but always this code gives me "#nav".</p>

<p>I fixed my problem by changing the code to:</p>

<pre><code> var current_anchor = $.url.attr('anchor');
 $.fn.vtabs = function() {
   alert("Your anchor is "+current_anchor);
 }
</code></pre>

<p>Now it always gives me the correct anchor. But I don't know why, and it appears messy to have that variable defined outside of the function.</p>
