Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Razor: call javascript function from view
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload