A day with .Net

My day to day experince in .net

XMLHttpRequest get server time

Posted by vivekcek on May 27, 2013

In your HTML put an anchor tag and div as given below.

<a href="#" id="timeLink">Click Get Time</a> 
<div id="timeDiv"></div>

Then in a script file add the below script and refer it in your page.

window.onload = function () {
    var link = document.getElementById("timeLink");
    link.onclick = getServerTime;
}

function getServerTime() {

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/Home/GetServerTime");
    xhr.onreadystatechange = function () {

        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var timeDiv = document.getElementById('timeDiv');
                timeDiv.innerHTML = xhr.responseText;
                
            }
        }

    }
    xhr.send();

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s