JavaScript – Pro9ramming https://pro9ramming.com Software craftsman's blog Wed, 15 Apr 2020 17:51:05 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 Matrix style JavaScript injection https://pro9ramming.com/matrix-style-javascript-injection/ Tue, 21 Mar 2017 10:42:28 +0000 http://pro9ramming.com/blog/?p=453 Who doesn’t like The Matrix !? Paste this into your URL bar and experience the magic:

data:text/html,<body%20style=margin:0><canvas%20id=q%20/><script>var%20q=document.getElementById('q'),s=window.screen,w=q.width=s.width,h=q.height=s.height,p=Array(256).join(1).split(''),c=q.getContext('2d'),m=Math;setInterval(function(){c.fillStyle='rgba(0,0,0,0.05)';c.fillRect(0,0,w,h);c.fillStyle='rgba(0,255,0,1)';p=p.map(function(v,i){r=m.random();c.fillText(String.fromCharCode(m.floor(2720+r*33)),i*10,v);v+=10;%20return%20v>768+r*1e4?0:v})},33)</script>

 

]]>
jQuery datetime picker over AJAX in ASP.NET MVC https://pro9ramming.com/jquery-datetime-picker-over-ajax-in-asp-net-mvc/ Fri, 17 Jul 2015 12:33:37 +0000 http://pro9ramming.com/blog/?p=429 Continue reading jQuery datetime picker over AJAX in ASP.NET MVC]]> jQuery UI has datepicker, but there is a separate plugin that selects both date and time here. This tutorial shows how to send datetime value over AJAX and call ASP.NET MVC action (over POST method).

First of all, a way for Date and Time formatting is needed, and excellent jQuery plugin for that can be found here. Date formatting is done by using $.format.date() function.

Following scripts (along with jQuery core library) have to be placed in HTML:

<script src="@Url.Content("~/Scripts/jquery.datetimepicker.full.js")"></script>
<script src="@Url.Content("~/Scripts/jquery-dateFormat.js")"></script>

And also CSS for datetime picker:

<link media="all" rel="stylesheet" type="text/css" href="@Url.Content("~/Content/jquery.datetimepicker.css")" />

Place HTML input and button:

<input id="datetimepicker" type="text" />
<button type="button" class="btn btn-primary" id="save-btn">Save</button>

The following JS code creates datetime picker and attaches onclick listener to button:

$('#datetimepicker').datetimepicker({ 
   inline: true, //not drop-down 
   value: new Date(), //current datetime 
   step: 30 // time by 30 minutes 
}); 

$('#save-btn').click(function () { 
var datetime = $('#datetimepicker').datetimepicker('getValue'), 
    formatted = $.format.date(datetime, 'dd.MM.yyyy HH:mm'); 
    $.post('@Url.Action("ChangeDateTime", "SomeController")', 
    { id: '@ViewBag.Id', datetime: formatted }, 
    function (result) { 
        //Completed code 
    }); 
});

Value that is sent to the “dd.MM.yyyy HH:mm” format. In ASP.NET MVC SomeController place this action:

[HttpPost]
public bool ChangeDateTime(SomeModel data)
{
   //Save to db
}

SomeModel is a POCO class which takes Id and DateTime value from client side:

public class SomeModel
{
    public string Id { get; set; }
    public string DateTime { get; set; }
}

 

]]>
Clock in JavaScript https://pro9ramming.com/clock-in-javascript/ Tue, 03 Mar 2015 05:43:27 +0000 http://pro9ramming.com/blog/?p=76 Continue reading Clock in JavaScript]]> This is a simple script to create a digital clock in JavaScript:

<script>
function clock() {
    var today = new Date(),
        h = addZero(today.getHours()),
        m = addZero(today.getMinutes()),
        s = addZero(today.getSeconds());
    document.getElementById('clock_div').innerHTML = h + ":" + m + ":" + s;
    setTimeout(clock, 1000);
}
function addZero(k) {
    if (k < 10) {
        return "0" + k;
    }
    return k;
}
</script>
<div id="clock_div">
   <script>
      clock();
   </script>
</div>

clock() function uses today’s date to get time parts. After that it calls setTimeout() function to delay it’s own execution by 1 second. It is basically an infinite recursion with time delay option. addZero() function adds “0” prefix if number is smaller than 10. clock() function is called on div load.

]]>