Simple Examples in Javascript

document.write is a simple function used to display the content to the browser and it also embeds HTML tags inside it and renders as HTML tags.

<html>
<script type=text/javascript>
document.write("Hello How are you");
document.write("<h1> This is header 1 </h1>");
document.write("<p> This is a new paragraph </p>");
</script>
</html>

Advertisements

Validating a Textbox

<html>
<head>
<script type="text/javascript">
document.write("Validating a Text Box<BR>");
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null || value=="")
    {
    alert(alerttxt);
    return false;
    }
  else
    {
    return true;
    }  }}
function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(email,"Email must be filled out!")==false)
  {
  email.focus();
  return false;}
  }
}
</script>
</head>
<body>
<form action="submit.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>
</html>

Detect Browser and its Parameters

<html>
<body>
<script type="text/javascript">
document.write("<p>Browser: ");
document.write(navigator.appName + "</p>");
document.write("<p>Browserversion: ");
document.write(navigator.appVersion + "</p>");
document.write("<p>Code: ");
document.write(navigator.appCodeName + "</p>");
document.write("<p>Platform: ");
document.write(navigator.platform + "</p>");
document.write("<p>Cookies enabled: ");
document.write(navigator.cookieEnabled + "</p>");
document.write("<p>Browser’s user agent header: ");
document.write(navigator.userAgent + "</p>");
</script>
</body>
</html>

Browser Detect in Java Script

<html>
<head>
<script type="text/javascript">
function detectBrowser()
{
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ((browser=="Netscape"||browser=="Microsoft Internet Explorer")
  && (version>=4))
  {
  alert("You are having a Recent Browser!");
  }
else
  {
  alert("It’s time to upgrade your browser!");
  }
}
</script>
</head>
<body onload="detectBrowser()">
</body>
</html>

Alert, Confirm and Prompt in Javascript

 

<html>
<script type=text/javascript>
function alertFn()
{
alert("Welcome to my Website");
}
function confirmFn()
{
confirm("Please Select yes or no");
}

function promptFn()
{
prompt("Enter your Name", "Pradeepkumar");
}

</script>
<body onload="alertFn()">
<input type=button Value="Click" onclick="confirmFn()">
<input type=button value="Click Me" onclick="promptFn()">
</body>
</html>