JavaScript Interview Questions and Answers (294) - Page 10

What does History.length property do?

It's an static property of History object which returns the number of URLs in the history list.In other words we can say that,The length property of the history object returns the number of elements in the history list.

Syntax:
history.length


For Example:
var count = history.length;
It will return number of pages or URLs count which are visited by users.
What is the use of Window.Open method in Javascript?

Window.Open opens a new window and loads the document specified by a given URL or navigates the app window to the specified location.
It's an static method of window object.

Syntax:
window.open(URL,window_name,features,replace);

Where,
URL - can be any URL or name of the ASPX/HTML page.
Window_Name - can be any name.
Features - can be any feature.
Replace - can be any boolean value .i.e. True/False.
What are the features available in Window.Open method?

Following are the features available in Window.Open method:

1. fullscreen = { yes | no | 1 | 0 } -> Specifies whether to display the browser in full-screen mode. The default is no.
2. height = number -> Sets the height of the window in pixels.
3. left = number -> Specifies the left position, in pixels.
4. location = { yes | no | 1 | 0 } ->Specifies whether to display the Navigation Bar. The default is yes
5. menubar = { yes | no | 1 | 0 } -> Specifies whether to display the Menu Bar. The default is yes.
What does Resizable feature do in window.open method?

Resizable feature of window.open method specifies whether to display resize handles at the corners of the window or in other window,we can say that Resizable allow/disallow the user to resize the window.The default is yes.If we specify Yes then we can increase/decrease window meaning that we can resize window to full screen.We can also provide 1 for Yes and 0 for No.

For Example:
window.open("http://www.yahoo.com", "my_win","resizable = 1");


//OR

window.open("http://www.yahoo.com", "my_win","resizable = yes");

How to move window to its desired location?

With the help of window.moveTo Javascript function,we can move any child window to a desired location.

For Example:
function open_popup()

{
var popup_window = window.open("http://www.dotnetfunda.com", "mywindow","width = 100,height = 100");
popup_window.moveTo(0,0);
}

How to close any opened window or child window?

With the help of Close method of window object,we can close any opened window.

Syntax:
window.close();

How to set your website as a homepage using Javascript?

<BODY>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// If it's Internet Explorer, use automatic link
// Be sure to change the "http://www.YourWebSiteHere.com\"
// to the URL you want them to bookmark.
if (document.all){
document.write('<A HREF="javascript:history.go(0);" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.YourWebSiteHere.com\');">');
document.write('<font size="5" color=6699FF face=arial><B>Click Here to Make My Web Page Your Homepage</B></font></a>');
}

// If it's Netscape 6, tell user to drag link onto Home button
// Be sure to change the "http://www.YourWebSiteHere.com\"
// to the URL you want them to bookmark.
else if (document.getElementById){
document.write('<a href="http://www.YourWebSiteHere.com">Drag this link onto your Home button to make this your Home Page.</a>');
}

// If it's Netscape 4 or lower, give instructions to set Home Page
else if (document.layers){
document.write('<b>Make this site your home page:</b><br>- Go to <b>Preferences</b> in the <B>Edit</B> Menu.<br>- Choose <b>Navigator</b> from the list on the left.<br>- Click on the <b>"Use Current Page"</b> button.');
}

// If it's any other browser, for which I don't know the specifications of home paging, display instructions
else {
document.write('<b>Make this site your home page:</b><br>- Go to <b>Preferences</b> in the <B>Edit</B> Menu.<br>- Choose <b>Navigator</b> from the list on the left.<br>- Click on the <b>"Use Current Page"</b> button.');
}
// End -->
</script>
<br />
<font face="Tahoma"><a target="_blank" href="http://www.javascriptfreecode.com/"><span style="font-size: 8pt; text-decoration: none">JavaScript Free Code</span></a></font>
How to create a message box when page gets opened?

<html>
<head>
<script language="javascript" type="text/javascript">
alert("Welcome to my site")
</script>
</head>
</html>
<font face="Tahoma"><a target="_blank" href="http://www.dotnetfunda.com/"><span style="font-size: 8pt; text-decoration: none">DotNet Funda</span></a></font>
How to restrict the user to use right-click

<!-- this script got from www.javascriptfreecode.com-Coded by: Krishna Eydat -->
<SCRIPT LANGUAGE="JavaScript">
var message="Function Disabled!";

function clickIE() {if (document.all) {alert(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {alert(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
// -->
</script>
<font face="Tahoma"><a target="_blank" href="http://www.dotnetfunda.com/"><span style="font-size: 8pt; text-decoration: none">DotNet Funda</span></a></font>
How to set the color to a scrolll bar?

<STYLE>BODY {
SCROLLBAR-FACE-COLOR: red; SCROLLBAR-HIGHLIGHT-COLOR: gray;

SCROLLBAR-SHADOW-COLOR: black; SCROLLBAR-ARROW-COLOR: gray;

SCROLLBAR-TRACK-COLOR: black; SCROLLBAR-DARKSHADOW-COLOR: red
}
</STYLE>

<font face="Tahoma"><a target="_blank" href="http://www.dotnetfunda/"><span style="font-size: 8pt; text-decoration: none">DotNet Funda</span></a></font>
What does Next property do in History object?

The Next is also an static property of History object which contains the complete URL of the next element in the History list.This functionality or the URL visited is the same as pressing the forward button or menu.

In other words,we can say that,it returns the URL of the next document in the history array.

Syntax:
history.next

What does Previous property do in History object?

The Previous is also an static property of History which contains the complete URL of the previous element in the History list.This functionality or the URL visited is the same as pressing the back button or menu.

In other words,we can say that,it returns the URL of the previous document in the history array.

Note:This property has no default value.

Syntax:
history.previous

What does History.Current property do in Javascript?

The History.Current is also an static property which contains complete URL of the current History entry.

In other words,we can say that,it returns the URL of the current document in the history array.

Note: 1). This property has no default value.
2). This property is read-only.

Syntax:
history.current

What do we mean by History.Back method in Javascript?

The History.Back() is an static method which loads previous URL from the history list.This is the same as clicking the "Back button" in our browser.

Note: This method will not work if the previous page does not exist in the history list.

Syntax:
history.back()

What do we mean by History.Forward method in Javascript?

The History.Forward() is an static method which loads the Next URL from the History list.The functionality is the same as pressing the forward button of the browser.

Note: This method has no parameters defined.

Syntax:
history.forward()

What are the two ways of making an object stylish?

<html>
<head>
<style type="text/css">
.Headstyle {
color: #0000FF;
background-color: #C0C0C0;
}
</style>
</head>
<body>
<div>
<span class="Headstyle">This text uses Head style!</span></div>
<div>
<span style="color: #993399;background-color: #FFFF00;">This text uses Inline style!</span></div>
<br>
</body>
</html>

<font face="Tahoma"><span style="font-size: 8pt; text-decoration: none"><a target="_blank" href="http://www.dotnetfunda.com/">DotNetFunda</a></span></font>
When should a search engine come back?

By this meta tag, you say to the search engines when they come back and index your page,

<meta name="revisit-after" content="7 days">

<font face="Tahoma"><a target="_blank" href="http://www.dotnetfunda.com/"><span style="font-size: 8pt; text-decoration: none">DotNetFunda</span></a></font>
How to display the date and time when the page gets loaded?

<SCRIPT LANGUAGE = "Javascript">
var today= new Date() </SCRIPT>
....
<BODY onload=alert(today)>
....
</BODY>

In this, the event handler onLoad is embedded within the tag. OnLoad activates whenever a new page has finished downloading. This alert code tells the browser to create an alert box containing the value of the variable "today". Thus, it automatically prints out the date and time when the page has finished loading and requires no special action by the user.
How can a user assign a property dynamically in javascript and generate a page?

<HTML>

<HEAD>
<SCRIPT LANGUAGE = "Javascript">
document.firstline = "Welcome to this page";
</SCRIPT>
<TITLE>load demo</TITLE>
</HEAD>

<BODY>
<SCRIPT>
document.open();
document.write(document.firstline);
document.open();
</SCRIPT>

How to perform sum of digits from 1 to n in javascript?

<SCRIPT>

function sumDigits(num) {
var i, sum = 0;
//can declare two variables at once

for (i = 1; i <= num; i++) {
sum += i; // add each number to sum (ie, 1 + 2 + ...+ num)
}

// Display result
alert("The sum of the digits from 1 to "+ num + " is:\n\n\t " + sum);
}
</SCRIPT>

<BODY>

Looping Functions - Calculate the sum of the digits.

<FORM NAME="SumNums">
The sum of the digits from 1 to:
<INPUT TYPE="text" NAME="charNum">
<INPUT TYPE="button" VALUE="Calculate"
onClick="sumDigits(SumNums.charNum.value)">
</FORM>
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