Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

Tuesday, December 5, 2017

How to convert a josn array to object array in JavaScript.


To accomplish the same we could get use of the $.map function



Below code snippet will convert  the json in jsondata variable to JavaScript array



You can see the result in the console screen of browser.

var o = [{"Rating":"Negative","Value":1.0},{"Rating":"Neutral","Value":1.0},{"Rating":"Postive","Value":3.0}];

var arr = $.map(o, function(el) { return [[el.Rating,el.Value]]; })

console.log(arr)

https://jsfiddle.net/92e03nqu/

Thursday, May 22, 2014

Javascript to detect whether user is viewing the page

Today I was writing a javascript to check whether there is a user activity on the page or not. Thought of putting the same to blog so that some on else can use of it.

Here is the code for it......

<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script>
    //Assinging the setInterval for function time()  for every 1 seconds, used to show time
    var myVar = setInterval(function () { time() }, 1000);
    //assinging the setInterval for function reset() for every 5 seconds, used to reload page
    var myVarReset = setInterval(function () { reset() }, 5000);
   
    //Function to show the time and will be update in nevery second
    function time() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        $('#time').text(t);
    }

    //Function to reload the page which is set to call every 5 seconds using  setInterval
    function reset()
    {
        location.reload();
        alert("Reset");
    }
    //Handling the onmousemove event of the form
    window.onmousemove=function()
    {
        //clering the setInterval for function reset()
        clearInterval(myVarReset);
        //Againg assinging the setInterval for function reset()  for every 5 seconds
        myVarReset = setInterval(function () { reset() }, 5000);
    }
</script>
<text id="time"></text>

So how does this code works?

First of all we need to understand what does setInterval and clearInterval does?

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.
The ID value returned by setInterval() is used as the parameter for the clearInterval() method.
Tip: 1000 ms = 1 second.

Tip: To execute a function only once, after a specified number of milliseconds, use the setTimeout() method.

Let’s look into the  ode in detail……………….

    var myVar = setInterval(function () { time() }, 1000);
    var myVarReset = setInterval(function () { reset() }, 5000);
In this part we are using setInterval to set two functions “time” and “reset”, one for showing the current time on the page and the other for reloading the page if there is no user activity for 5 seconds. The output of these factions are assigned to variable myVar and myVarReset respectively.

    function time() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        $('#time').text(t);
    }
This part is responsible to show the current time on the page , which will be called every 1 second  as defined by the above setInterval

    function reset()
    {
        location.reload();
        alert("Reset");
    }
This function just reloads the page

    window.onmousemove=function()
    {
        clearInterval(myVarReset);
        myVarReset = setInterval(function () { reset() }, 5000);
    }

Here we handle the onmousemove on the page and resets the setinterval for reset() function using the clearInterval by passing variable myVarReset.

Once it is reset , the function reset() is again set to call eve very 5 seconds using setInterval.


Saturday, September 3, 2011

How to select an option in dropdown by its index using JavaScript?

The below code will help you to select an option in a dropdown by its index


You can find more examples of the same in the below website

http://www.mredkj.com/tutorials/tutorial003.html

Friday, May 28, 2010

How to initiate a JavaScript function after an Ajax post back or Page load?

The below code will explain you how to call a function after an Ajax post back using Sys.Application.add_load.you can hook a fuction to this evvent with will be called after a page is loaded after ajax postback.
 

How to select an item in a dropdown (combobox) by its values using JavaScript?

You can select an item a by its values using the below code.


You can also get more information regarding various function and propertied of a dropdown (combobox) from the below link. http://www.navioo.com/javascript/dhtml/Changing_Select_Element_Content_two_Combobox_2755.html

How to get the selected values of dropdown (combobox) using JavaScript?

The selected values of a dropdown (combobox) can be accessed by the values and text property of a dropdown’s (combobox) option object.

The option object has the following properties.

defaultSelected          Refers to the option that is selected by
                                 default from the select box.

index                         Refers to the zero-based indexed location
                                of an element in the Select.options array.

selected                     Refers to the selected value of
                                 the select box.

text                            Refers to the text for the option.

value                          Refers to the value that is returned
                                  when the option is selected.

Accessing the value of a dropdown (combobox) is shown in the below example.


You can also get more information regarding various function and propertied of a dropdown (combobox) from the below link.
http://www.navioo.com/javascript/dhtml/Changing_Select_Element_Content_two_Combobox_2755.html

Thursday, May 27, 2010

Date Functions in Java Script?

To handle the date time function in javascript ,i sugesst to use the pre coded fucntion in the web site below.You can use the function in the below site free of cost in your code.

Thre are also other usefull javascript function in this site.

http://www.mattkruse.com/javascript/date/

Monday, April 26, 2010

How to hide Validation summary at client side using Java Script?

The below script will help you to hide Validationsummary at client side .

Friday, April 23, 2010

How to assign value to asp.net literal or Label using JavaScript?

For an asp.net Label you can assign value using the innerText property


For an asp.net Literal you can assign value using the innerHTML property