Javascript

    How to convert JSON to string or string to JSON

    To convert string to JSON use
      var jsonObject = JSON.parse(string);
    To convert JSON to string use:
    var dataString = JSON.stringify(jsonObject);

    How to find and replace in an array in underscore js

    To find and replace in an array using underscore js find function. There are many different ways but i use this approach because it find stops traversing the list when it finds the element
     
    var newArray = [{name: "mohit", age: 5},{name: "amar", age:15},{name: "raj", age: 50},{name: "chop", age: 25},{name: "nini", age: 45}];
        _.find(newArray, function(item, index) {
           if (item["name"]  == "mohit") {
               newArray[index] = {name: "chopper", age: 58};
               return true;
           } else {
               return false;
           }
        });

    How to call functions inside javascript loop?

    var functions = [];
    var invitees = ["test1@mohitsharma.net', "test2@mohitsharma.net", "test3@mohitsharma.net"];
    for (var i in invitees) {
                 var email = invitees[i];
                 functions.push(function(email) {return function() {
                    // do changes with email here
                  }; }(email));
    }

    // Execute function array according to your requirement or instead of pushing function to array you can call them directly

    How to write source code in html pages

    to write source code in html pages use this plugin

    https://github.com/timheap/SecretSource

    How to calculated age in javascript between two dates

    Use below function to calculate age var currYear = d.getFullYear(), currMonth = d.getMonth(), currDay = d.getDate(); var checkAge = function(birthMonth, birthDay, birthYear) { var age = currYear - birthYear; if (currMonth < birthMonth - 1) { age--; } if (birthMonth - 1 == currMonth && currDay < birthDay) { age--; } return age;

    How to create remember me in angular js

    Create a service to add/remove cookie define( ['app'], function (app) { app.factory('$remember', function() { function fetchValue(name) { var gCookieVal = document.cookie.split("; "); for (var i=0; i < gCookieVal.length; i++) { // a name/value pair (a crumb) is separated by an equal sign var gCrumb = gCookieVal[i].split("="); if (name === gCrumb[0]) { var value = ''; try

    How to validate international phone number using javascript

    You can do this in following way using the following javascript function pass the phone number you can use your custom code instead of alert messages
    function val_phone_number (number) {
        var regexp = /^\+(?:[0-9] ?){6,14}[0-9]$/;
        if (regexp.test(number)) {
           alert("Phone number is valid");
        } else {
           alert("Phone number is invalid");
        }
    }
    Note: The number starts with (+) plus sign, followed by the country code and number. For e.g : +65 6538 1111

    How to print gmap ?

    You can print gmap using javascript by using following procedure. First of all create html file printmap.html and place it in the root directory or on the path that is accessible.
    <script type="text/javascript">
      var map_content =  window.opener.document.getElementById("mapDiv");
      document.write(map_content.innerHTML);
      window.print();
    </script>
    On the page where we have gmap place a link or button as needed I'm using simple link here mapDiv is the div id in which the map is placed

    How to open javascript popup for 5 seconds