No it can be done purely in Razor since it's a serverside language. I would recommend using JavaScript purely and the link seems to be a nice tutorial on how to do it.
You could use Razor in to fetch the datetime from the server using an AJAX approach but it seems a bit overkill when you can do it with pure JavaScript.
I just realised something....where do I put the javascript file? I tried in the scripts folder because it's possible to set the file as a js file and then on the Master template I ran <span id="http://localhost:12718/scripts/clock.js"></span> which doesn't work. Am I miles off?
There is no need to create the script within Umbraco if you have access to the disk.
You need to copy the javascript code into a script file, that you can save in the /scripts folder on your disk. You could also have it in /assets/javascript if you would like. It does not matter.
When you save your script file you can save it as "clock.js". Now you need to reference "clock.js" in the bottom of your master template before the ...
Display Current Time
Hi All
Is there a way to display the current time. I have @DateTime.Now but that only updates when I refresh the screen. Is there a way to make it current?
Thanks.
Tony
Hi Tony
Do you need to have it update dynamically without a page reload? Like this for instance? http://perishablepress.com/unobtrusive-javascript-dynamic-clock/ Or what do you have in mind?
/Jan
Hi Jan
That's exactly what I had in mind. Can this be done in Razor or is this the easiest way?
Thanks.
Tony
Hi Tony
No it can be done purely in Razor since it's a serverside language. I would recommend using JavaScript purely and the link seems to be a nice tutorial on how to do it.
You could use Razor in to fetch the datetime from the server using an AJAX approach but it seems a bit overkill when you can do it with pure JavaScript.
Hope this helps.
/Jan
Hi Jan
OK I'll follow the tutorial. Thanks a lot.
Tony
Hi Jan
I just realised something....where do I put the javascript file? I tried in the scripts folder because it's possible to set the file as a js file and then on the Master template I ran <span id="http://localhost:12718/scripts/clock.js"></span> which doesn't work. Am I miles off?
Thanks.
Tony
Hi Tony,
If you have your script in the script folder in Umbraco, then you should called it like this
Hope this helps,
/Dennis
Hi Dennis
Thanks, but no joy. The area where I'm expecting the clock is blank, so maybe I'm not calling it correctly.
Tony
Hi Tony,
Okay, I have made a screenshot so you can see where to find the path to file in the script folder. See the screenshot below.
And the way that you include the script is like:
Hope this helps,
/Dennis
Thanks Dennis, it's still blank. I'll have another look tomorrow, with fresh eyes and a clear head!
:)
Hi Tony
You could also in your razor code use DateTime.Now: http://msdn.microsoft.com/en-us/library/zdtaw1bw%28v=vs.110%29.aspx
and then use Moment.js to update the time, which trigger for an eventhandler, e.g. click or just after DOM is ready.
if you use jQuery, something like this:
/Bjarne
Hi Tony
There is no need to create the script within Umbraco if you have access to the disk.
You need to copy the javascript code into a script file, that you can save in the /scripts folder on your disk. You could also have it in /assets/javascript if you would like. It does not matter.
When you save your script file you can save it as "clock.js". Now you need to reference "clock.js" in the bottom of your master template before the ...
so try writing it like this
Does this make sense? :)
/Jan
Hi All
Thanks for all the help. What a great community! :)
@Bjarne I don't know jquery, it is something I should probably get on top of, so thanks for the links.
@Jan I added the last bit of code, and hey presto, we have a clock! And it works! I did have a clock before, but it didn't work!!
Thanks.
Tony
Hi Tony
Glad you managed to get it working :)
Have fun developing.
/Jan
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.