Java Script Code Sample
Java Script Code Sample
An object in the context of JavaScript, is a collection of properties and methods consisting a set of defined
characteristics that you can view and modify, and which you can interact. Methods are the techniques that are
used to perform action involving objects and properties. For example statement like Document. Write("Welcome to
Javascript") here Document is the object, Write is the method of Document object, JavaScript follows event driven
sequence. the events determine the program flow and event handlers determine what happen when these event
occurs.
Javascript support both built in objects as well as you can create your own 5. Java Script FAQ
custom objects, there are more than 20 objects, in these section we know in
details about them.
JavaScript's built in objects from the core of the logical event-driven environ meant within which scripts are
designed. These objects (frames, windows, documents, forms, and son on) behave in predictable ways and can be
manipulated through their well-defined properties and methods. Built in objects are those that are automatically
included in the java script environment; you don not have to create them or do anything to make them accessible
other refer to them within a script whenever they are nodded.
<html>
<body>
<script language="JavaScript">
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">This is header " + i)
document.write("</h" + i + ">")
}
</script>
</body>
</html>
Example 44. Button which displays source of the web page Go Top
<html>
<head>
<script language="JavaScript">
function source()
{
location="view-source:" + window.location.href
}
</script>
</head>
<body>
<form>
<input type="button" value="View source" onclick="source()">
</form>
</body>
</html>
Example 54. Converts the page into without frames when clicked the button Go Top
<html>
<head>
<script language="JavaScript">
function breakout()
{
if (window.top != window.self)
{
window.top.location="tryjs_breakout.htm"
}
}
</script>
</head>
<body>
<form>
To break out of the frame:
<input type="button" onclick="breakout()" value="Click me">
</form>
</body>
</html>
Example 71. Displaying message to the user depending on the browser used by the client
<html>
<head>
<script language="JavaScript">
function browserversion()
{
txt="Your Browser is unknown"
browser=navigator.appVersion
if (browser.indexOf("2.")>-1)
{
txt="Your Browser is from the stone-age"
}
if (browser.indexOf("3.")>-1)
{
txt="You should update your Browser."
}
if (browser.indexOf("4.")>-1)
{
txt="Your Browser is good enough"
}
document.forms[0].message.value=txt
}
</script>
</head>
<body onload="browserversion()">
<form>
<input type="text" name="message" size="50">
</form>
</body>
</html>