title: Define a global variable in a JavaScript function
body: <p>Is it possible to define a global variable in a JavaScript function?</p>
  <p>I want use the <code>trailimage</code> variable (declared in the <code>makeObj</code> function) in other functions.</p>
  <pre><code>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
      &lt;head id=&quot;Head1&quot; runat=&quot;server&quot;&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;script type=&quot;text/javascript&quot;&gt;
              var offsetfrommouse = [10, -20];
              var displayduration = 0;
              var obj_selected = 0;
              function makeObj(address) {
                  **var trailimage = [address, 50, 50];**
                  document.write('&lt;img id=&quot;trailimageid&quot; src=&quot;' + trailimage[0] + '&quot; border=&quot;0&quot;  style=&quot; position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + trailimage[1] + 'px; height: ' + trailimage[2] + 'px&quot;&gt;');
                  obj_selected = 1;
              }
  
              function truebody() {
                  return (!window.opera &amp;&amp; document.compatMode &amp;&amp; document.compatMode != &quot;BackCompat&quot;) ? document.documentElement : document.body;
              }
              function hidetrail() {
                  var x = document.getElementById(&quot;trailimageid&quot;).style;
                  x.visibility = &quot;hidden&quot;;
                  document.onmousemove = &quot;&quot;;
              }
              function followmouse(e) {
                  var xcoord = offsetfrommouse[0];
                  var ycoord = offsetfrommouse[1];
                  var x = document.getElementById(&quot;trailimageid&quot;).style;
                  if (typeof e != &quot;undefined&quot;) {
                      xcoord += e.pageX;
                      ycoord += e.pageY;
                  }
                  else if (typeof window.event != &quot;undefined&quot;) {
                      xcoord += truebody().scrollLeft + event.clientX;
                      ycoord += truebody().scrollTop + event.clientY;
                  }
                  var docwidth = 1395;
                  var docheight = 676;
                  if (xcoord + trailimage[1] + 3 &gt; docwidth || ycoord + trailimage[2] &gt; docheight) {
                      x.display = &quot;none&quot;;
                      alert(&quot;inja&quot;);
                  }
                  else
                      x.display = &quot;&quot;;
                  x.left = xcoord + &quot;px&quot;;
                  x.top = ycoord + &quot;px&quot;;
              }
  
              if (obj_selected = 1) {
                  alert(&quot;obj_selected = true&quot;);
                  document.onmousemove = followmouse;
                  if (displayduration &gt; 0)
                      setTimeout(&quot;hidetrail()&quot;, displayduration * 1000);
              }
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
          &lt;img alt=&quot;&quot; id=&quot;house&quot; src=&quot;Pictures/sides/right.gif&quot; style=&quot;z-index: 1; left: 372px;
              top: 219px; position: absolute; height: 138px; width: 120px&quot; onclick=&quot;javascript:makeObj('Pictures/sides/sides-not-clicked.gif');&quot; /&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>