JavaScript Interview Questions and Answers (294) - Page 6

How to pass asp.net Textbox control ID from code-behind to JavaScript function?

With the help of ClientId we can pass textbox control id from code-behind to Javascript function.

For Example:-

//Aspx Part
function get_value(id)

{
var value = document.getElementById(id).value;
alert(value);
}


//Code-Behind Part
ScriptManager.RegisterStartupScript(Page, typeof(Page), "message", "javascript:get_value('"+ txt_name.ClientID +"');", true);

How to append 2 strings?

NOTE: This is objective type question, Please click question title for correct answer.
What does isNaN function behave in Javascript?

It's a javascript in-built function which returns true,if expression or argument is not a number otherwise returns false.
Full form of isNan is Not a number.

For Example:-

document.write(isNaN("Vishal")+ "</br>");

document.write(isNaN("2013/12/10")+ "</br>");
document.write(isNaN(123)+ "</br>");

Output will be:-

true
true
false
What is the output of below code? <script type = "text/javascript" language = "javascript"> function check_value() { alert("1"+2+4); } </script>

Since 1 is in double quotation,so it will be treated as string,everything is a string over here,so the result will be 124.
What is the output of below Javascript function? <script type="text/javascript" language="javascript"> function get_value() { var value = Math.PI; alert(value); } </script>

Above function gives us "3.1415926".

Math is an in-built Javascript function as Dot Net have.

PI means 22/7 = 3.14;
How to get Browser version through Javascript?

With the help of navigator.appVersion property,we can
any get browser version.

<script type="text/javascript" language="javascript">

function get_browser_version()
{
var version = navigator.appVersion;
alert(version);

}
</script>
How to make all the character capitalize in Javascript?

NOTE: This is objective type question, Please click question title for correct answer.
How to access Session value in JavaScript?

Session as we know that,is used across the application and is used for storing any values.

Suppose,Session["Name"] is defined in Page Load Event

protected void Page_Load(object sender, EventArgs e)

{
Session["Name"] = "Vishal";
}


If we want to access Session["Name"] from inside Javascript,

function access_session_value()

{
var name = "<%=Session["Name"]%>";
alert(name);
}


Output would be Vishal
Can we access Viewstate in Javascript?

Yes,like Session we can also access Viewstate in Javascript.

For Example:-
function access_viewstate()
{
var name = "<%=ViewState["Name"]%>";
alert(name);
}

Where ViewState["Name"] is defined in Code behind page.
How to get current URL?

With the help of location.href property we can get current URL.

For Example:-

function get_url()
{
alert(location.href);
}

It will show the page URL.
How to get only Port from URL?

Use port proprty of location to get port from Location or URL.

For Example:-

function get_url_port()

{
alert(location.port);
}

What is the output of below Javascript Code? var fullname = "Vishal Kumar"; var character = fullname[7]; alert(character);

NOTE: This is objective type question, Please click question title for correct answer.
How to get Current date in Javascript?

With the help of getDate() function of Date() Object,we can get current Date.

function get_current_date() 

{
var date = new Date();
alert(date.getDate());
}


It will show current date.
How to get current Year in Javascript?

With the help of getFullYear() function of Date() Object ,we can get current Year.

function get_current_year() 

{
var date = new Date();
alert(date.getFullYear());
}


It will show current year as 2013.
What is the output of below Javascript code? function check_value() { var value = new Boolean(1); alert(String(value)); }

Output will be "true".

As we know that,Boolean data-type takes only True and False value.

The String() function converts the value of an object to a string.

and also true = 1 and false = 0 that's why it will return true.
What is the output of below Javascript code? function check_value() { var value = new Boolean(false); alert(String(value)); }

Output will be "false".

As we know that,Boolean data-type takes only True and False value.

The String() function converts the value of an object to a string. That's why it would be False.
How to Replace any Character or Word in Javascript?

With the help of Replace in-built Javascript Function,we can replace any Character or Word in Javascript.

For Example:-

function replace_word()

{
var text = "Vishal Kumar";
var result = text.replace("Kumar","Neeraj");
alert(result);
}


Here, Kumar will be replaced with Neeraj.So,the output would be Vishal Neeraj.
How to start Timer in Javascript?

With the help of setInterval static method of window object.
We can invoke or start timer.

Syntax:-

window.setInterval("javascript function",milliseconds);

For Example:-

var str = setInterval(function(){start_timer()},3000);


function start_timer()
{
alert("Hello");
}

Then it will shown Hello message in 3 seconds.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories