JavaScript Interview Questions and Answers (294) - Page 11

How can we get the length of the string in Javascript?

Use the length property of string to return the length.It's an in-built property of string object.
For Example:
alert("Hello".length);

Output will be 5.
What is the return type of length property of string in Javascript?

Length property always return integer value.Actually it count number of characters in a given string.
Which event do we use to raise Command Button Or Link Button event at client-side?

We use OnClientClick event to raise button event in client-side.In the OnClientClick event,we have to associate Javascript function which will fire when we click on Button.

For Example:-
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="fire_button_event();" />


<script type="text/javascript">
function fire_button_event()
{
alert('button click event fired');
}
</script>

What is the use of Attributes property?

Attributes property is used to associate any Javascript function on server side.It is used with controls like command button,link button and so on.
By using this property,we can fire any JS function in server side.

For Example:-
On code behind on Page load event we can write:
Button1.Attributes.Add("onclick", "js function();");

How many parameters Attributes function take?

It has 2 parameters:
1st parameter takes event which will be associated with any control like button,checkbox or any controls and,
2nd parameter takes any javascript function.
Which event Checkbox is associated on server side?

Checkbox has OnCheckedChanged event which we call when we check or un-check checkboxes.

For Example:-
<asp:CheckBox ID="CheckBox1" runat="server" Text="Yes" OnCheckedChanged="CheckBox1_CheckedChanged"/>


protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
//write your code
}

Which event will be used for Checkbox in client-side?

When we want to perform any javascript function with checkboxes,then use onclick event for firing checkboxes event.

For Example:-
<asp:CheckBox ID="CheckBox2" runat="server" Text="Yes" onclick="js_function();"/>

How to align Checkboxes?

We have TextAlign property of Checkboxes which is used to align Checkboxs.

For Example:-
<asp:CheckBox ID="chkbox_yes" runat="server" Text="Yes" TextAlign="Left"/> 

How will we change border color through Javascript?

We will write below code in javascript function to change border color
document.getElementById("div1").style.borderColor  = "green";

document.getElementById("btn_save").style.borderColor = "blue";

What is the default value for borderColor property?

Black is the default value for borderColor property means if we do not provide any color to borderColor property,then it takes black color as default.
What is the return type of borderColor property?

BorderColor return type is always String data type.
How to disable browser's Back button?

We have to write only one line of code in below Javascript function to disable back button of browser:-
<script type="text/javascript" language="javascript">

function disable_browser_back_button()
{
window.history.forward();
}
</script>

And call above function on Body Onload event as
<body onload="disable_browser_back_button();">

How to disable both Back and Forward Browser buttons?

Write only one line of code in below Javascript function:-
<script type="text/javascript" language="javascript">

function disable_browser_buttons()
{
window.history.forward(1);
}
</script>

<body onload="disable_browser_buttons();">

Note:- But i would not suggest you to use this JavaScript,Because if the JavaScript is Disable
in the Browser,then above code will not work.

How to disable Enter key pressed inside TextBox to avoid submission or postback through Javascript?

Write below Javascript function and call below function on onkeydown event in textbox definition as:-
function prevent_submission()

{
return (event.keyCode!=13);
}
<asp:TextBox ID="txt_emp_first_name" runat="server" onkeydown = "prevent_submission();"></asp:TextBox>

In Javascript, how to redirect another page from the current page?

Here in javascript, we should use location.href that should be done by alert method. If we want to get the current url of the web page from this we can set location.href if we want to redirect current page in to another page.

Example:
<script type="text/javascript">

function TestFunction(){
alert(location.href);
location.href="http://www.google.com";
}
</script>
<input type="Button" id="btntest" onclick="TestFunction()" value="clickme"

What is Provider? What are different method Providers in angularJS?

Provider is used to get values from the external sources by means of RESTful services.
AngularJS uses $provide to register new providers.

The $provide has six methods to create custom providers.
*.constant
*.value
*.service
*.factory
*.decorator
*.provider
Using JavaScript how do we show an alert to the user?

In this case, we should use alert method... to alert user with some information in JavaScript. It is an important method for every Java script to show an alert information on the page.


Example Script:
<script type="text/javascript">

function confirmMe() {
var v = confirm("Are you sure to confirm?");
if (v) {
//ok pressed; user confirmed to proceed further
//write code to execute code
alert("Go ahead");
}
else {
//cancel pressed; user don't want to proceed
alert("Stop")
}
}

</script>


Html
<input type="button" onclick="confirmMe()" />

What can javascript programs do?

Java script is used to validate the user input with simple calculations.Generate the webpages with out hitting the web server.java script will reduce the burden of web server and increase the performance of application.
What does isNan() function do?

isNan() is a javascript function. It allows only numeric values.
It returns true when the given input is not a number.
What does "1"+2+3 evaluates to?

The Answer is 123.
Because in the above question "1" is string. In java script, when string occurs in the first then it feels everything is string.
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