Friday, 3 April 2015

Ajax Interview Questions and Answer for 1 Year Experienced




Ajax Interview Questions and Answer for Experienced

Question: How to Abort ajax requests using jQuery?
Use abort() function to abort a ajax request.
See Example Below:
var ajaxRequest = $.ajax({
    type: "POST",
    url: "/ajax",
    data: "name1=value1&name2=value2",
    success: function(msg){
       //here success comes
    }
});

//Abort the ajax request
ajaxRequest.abort()

When you abort the ajax call, you can see ajax request as aborted in browser console.


Question: How to redirect a page after success of Ajax call?
You can use window.location.href, to redirect.
See Example Below:
$.ajax({
    type: "POST",
    url: "/ajax",
    data: "name1=value1&name2=value2",
    success: function(msg){
        if(msg.success){
            window.location.href='/newpage.php';
        }
       //here success comes
    }
});



Question: How do I test an empty object?
You can use jQuery isEmptyObject().
See Example Below:
var myArray=new Array();
console.log($.isEmptyObject(myArray));//return true

var myArray=new Array('This is message');
console.log($.isEmptyObject(myArray));//return false


Question: How to submit a form in Ajax?
We can use jQuery ajax function to submit a Form.
See Example Below:
Following is Javascript function
   function onSubmitFunction(){
    $.ajax({
        type: "POST",
        url: "/ajax",
        data: $('#mytestForm').serialize(),
        success: function(msg){
            if(msg.success){
               //here success comes
            }

        }
    });
    return false;
}

Following is HTML Code
<form id="mytestForm" onsubmit="return onSubmitFunction()">
Name: <input name="name" type="text" /><input name="submit" type="submit" value="Submit" />
</form>



Question: How to get the value of textarea?
You can use val function of jQuery to get the text of textarea.
See Below:
console.log($('textarea#idOfTextArea').val());


Question: How to send AJAX call without jQuery?
For sending ajax call in IE7+, Firefox, Chrome, Opera, Safari
XMLHttpRequest
For sending ajax call in IE
use ActiveXObject

See Example Below:
function callAjax(url,method) {
    var xmlhttp;
    if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
            // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {

                //console.log(xmlhttp.responseText);
            }
        }
    }

    xmlhttp.open(method, url, true);
    xmlhttp.send();
}
callAjax('/ajax?data=1','GET')


Question: How to make all ajax call cache Free?
$.ajaxSetup ({
    // Disable the caching of AJAX responses for all Ajax
    cache: false
});


Question: How to download file?
very simple
window.location="/folder/file.pdf"


Question: How can I add a custom HTTP header in Ajax?
Just Use the header parameter to set the custom header
$.ajax({
    url: '/ajax',
    headers: { 'x-my-custom-header': 'I am good' }
});



Thursday, 2 April 2015

Javascript Interview Questions and Answers for Experienced 2

Javascript Interview Questions and Answers for Experienced 2



Question: How to set a default parameter value for a JavaScript function?
/** Here email is parameter in which we have set the default value i.e email@domain.com **/
function function1(name, email)
 {
   email = typeof email !== 'undefined' ? email : 'defaultemail@domain.com';
    console.log('name='+name+', Email= '+email);
 }

function1('john','myname@gmail.com');
function1('john');



Queston: How to convert a string to lowercase?
var str='This is testing String';
str = str.toLowerCase();
console.log(str);



Question: How to modify the URL of page without reloading the page?
use pushState javascript function.
For Example:
window.history.pushState('page2', 'This is page Title', '/newpage.php');


Question: How to convert JSON Object to String?
var myobject=['Web','Technology','Experts','Notes']
JSON.stringify(myobject);


Question: How to convert JSON String to Object?
var jsonData = '{"name":"web technology","year":2015}';
var myobject = JSON.parse(jsonData);
console.log(myobject);


Question: How to check an variable is Object OR String OR Array?
Use below function to get Data type of javascript variable.
function checkDataType(someVar){
 result ='String';
    if(someVar instanceof Object){
       result ='Object'
    }
    if($.isArray(someVar)){
      result = 'Array';
    }
return result;
}

var someVar= new Array("Saab", "Volvo", "BMW");
console.log(result);



Question: Can i declare a variable as CONSTANT like in PHP?
No, I think cosntant not exist in javascript.
But you can follow same type convention to declare constant.
var CONSTANT_NAME = "constant value";


Question: How to open URL in new tab in javascript?
use javascript, window.open function.
window.open('http://www.web-technology-experts-notes.in/','_blank');


Question: What is difference between undefined and object?
undefined means some variable's value is not defined yet.
object means variables's value is defined that is either function, object OR array.

With use of below, you can easily determine whether it is object OR NULL.
console.log(typeof(null));      // object
console.log(typeof(undefined)); // undefined


Question: How to get current date in JavaScript?
var today = new Date();
console.log(today);



Question: How do I declare a namespace in JavaScript?
var myNamespace = {

    function1: function() {   },

    function2: function() {    }

    function3: function() {    }
};

myNamespace.function3();


Question: What is the best way to detect a mobile device in jQuery?
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {

}



Question: How to detect mobiles including ipad using navigator.useragent in javascript?
 if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ||  navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone/i)){
        console.log('Calling from Mobile');      
    }else{
    console.log('Calling from Web');      
}



Question: How to detect mobiles including ipad using navigator.useragent in javascript?
 if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ||  navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone/i)){
        console.log('Calling from Mobile');      
    }else{
    console.log('Calling from Web');      
}