title: MVC3 Razor: call javascript function from view
body: <p>I am new in MVC3 Razor and want to show running time on a view (index.cshtml). 
  I use a javascript function and put it in _Layout.cshtml so all other "home" views can use it (see following snippet)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;title&gt;
          @ViewBag.Title
      &lt;/title&gt;
      &lt;link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /&gt;
      &lt;script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;script type="text/javascript"&gt;
      var uhr = new Date();
      var minuten;
      var stunden;
      var sekunden;
      var interval = 500;
  
      function datum(id) {
          uhr.setTime(uhr.getTime() + interval);
          window.setTimeout(function () { datum(id) }, interval);
          minuten = uhr.getMinutes();
          stunden = uhr.getHours();
          sekunden = uhr.getSeconds();
          if (minuten &lt; 10) { minuten = '0' + minuten; }
          if (sekunden &lt; 10) { sekunden = '0' + sekunden; }
          if (stunden &lt; 10) { stunden = '0' + stunden; }
          document.getElementById(id).innerHTML = 'Jetzt ist: ' + stunden + ':' + minuten + ':' + sekunden;
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>My questions:
  1. how can I call this function (e.g. datum("uhr")) from index.cshtml? My try (see following) doesn't work:</p>
  
  <pre><code>@section SideBar {
      &lt;p&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href="Index.cshtml"&gt;Add Job&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="About.cshtml"&gt;About&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/p&gt;
      &lt;p&gt;
          The time is: datum("uhr");
      &lt;/p&gt;
  }
  </code></pre>
  
  <ol>
  <li>Any other "better" way?</li>
  <li>Is it a good practice? I am not sure if it is correct putting javascript function in _Layout.cshtml.</li>
  </ol>
  
  <p>Thx in advance.</p>