Free Guides

Language Tutorials


           

 

JavaScript

          Index         

Your Ad Here

 

Introduction

  ::Return to index::               <<  Previous Chapter                   Next Chapter >>

JavaScript is popular as a client side script in the web programing area. Java Script code gets execuated by a web browser. Here we will learn different types of codes in Java Script.
 

Enable JavaScript and detect disable setting of client browser

Are you using a browser that doesn't support JavaScript?

If your browser does not support JavaScript, you can upgrade to a newer browser, such as Microsoft Internet explorer 6 or Netscape 6.

Have you disabled JavaScript?

If you have disabled JavaScript, you must re-enable JavaScript to use this page. To enable JavaScript:

Using Internet Explorer 6

  1. On the Tools menu, click Internet Options.
  2. Click the Security tab.
  3. Click Custom Level.
  4. Scroll to Scripting. Under Active scripting, click Enable.
  5. Click OK twice.

Using Netscape 6

  1. On the Edit menu, click Preferences.
  2. Click Advanced.
  3. Select the Enable JavaScript for Navigator check box.
  4. Click OK.

Redirecting Browser to a page if JavaScript support is not there

If you have developed a page which depends on JavaScript for form validation or for any other purposes then you would be interested in detecting the setting of the client browser and would like to redirect to a different page explaining how to enable or disable JavaScript. Here we will discuss how to check this setting and redirect to a different page accordingly. We can detect this by using noscript tag and if the JavaScript is disabled then the code within this noscript tag will be executed. Here is the code to do that. This code will detect if the script setting is disabled and will redirect to a page explaining how to enable or disable JavaScript with meta refresh in 2 seconds.

<noscript>

<meta http-equiv="refresh" content="2; URL=enable_javascript.php">
<center>
<table cellpadding="0" cellspacing="0" border="0" width="550">
<tr><td width="100%" valign="top" class="PPDesTxt"><b>Are you using a browser that doesn't support JavaScript?</b></td></tr>

<tr><td width="100%" valign="top" class="PPDesTxt">
If your browser does not support JavaScript, you can upgrade to a newer browser, such as <a href="http://www.microsoft.com/windows/ie/downloads/ie6/default.asp">Microsoft&reg; Internet Explorer 6</a> or <a href="http://wp.netscape.com/computing/download/bdp/index.html">Netscape 6</a>.</td></tr>

<tr><td width="100%" valign="top" class="PPDesTxt"><b>Have you disabled JavaScript?</b></td></tr>

<tr><td width="100%" valign="top" class="PPDesTxt">If you have disabled JavaScript, you must re-enable JavaScript to use this page. To enable JavaScript:</td></tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" width="100%">

<tr><td width="100%" colspan="2"><img src="/images/T.gif" width="1" height="25" border="0"></td></tr>
</table></td>
</tr><tr><td>

</noscript>

Detecting the browser used by the visitor

There are different types of browsers with different versions used by the visitor to access a website. For a server side program there is nothing to do or special care to be taken as the code remains in severs side. However for client side tags or scripts as they gets executed at visitor end they have to give equal output for different browsers used by visitors.

The main problem is all browsers are not equal and they behave differently for different scripts. Some of the differences are kept intentionally by the browsers. So at the developer end it became difficult to develop a common code. So we ( as developer ) have to find out the visitor browser details and accordingly execute the part of the code specially written for the browser. Now how to know what is the browser being used by the visitor?

The solution is navigator object

The navigator object used to detect different properties of the browser. All properties are again not supported by all the browser but by using some common properties we can identify the browser used. We will discuss about the properties of this navigator object and the outputs we will get for different browser.

Here are the navigator property values for your browser ( The code is below that )


 

UserAgent Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
appName Microsoft Internet Explorer
appVersion 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
browserLanguage en-us
platform Win32

Here is the code which display above details about your browser

<table width='100%' border='0' cellspacing='1' cellpadding='0'>

<script type="text/javascript">
var st;
st="<tr><td class=data><b>UserAgent </b></td><td class=data>"+ navigator.userAgent + "</td></tr>";
st=st + "<tr><td class=data><b>appName </b></td><td class=data>"+ navigator.appName + "</td></tr>";
st=st + "<tr><td class=data><b>appVersion</b></td><td class=data>"+ navigator.appVersion + "</td></tr>";
st=st + "<tr><td class=data><b>browserLanguage</b></td><td class=data>"+ navigator.browserLanguage + "</td></tr>";
st=st + "<tr><td class=data><b>platform</b></td><td class=data>"+ navigator.platform + "</td></tr>";

document.write(st);
</script>
</table>

Now let us write some code to identify the browser and develop the code specific to that.

<script type="text/javascript">
var st;
st="";
if(navigator.userAgent.indexOf("Firefox")!=-1){
// Keep the code for Firefox
st=st + "Firefox";
}
else if (navigator.appName.indexOf("Internet Explorer")!=-1){
st=st + "Internet Explorer";
// Keep the code for Internet Explorer
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
st=st + "Opera";
// Keep the code for Opera
}
document.write(st);
</script>

 

document.URL of a page

The full URL of the page can be collected by using document object in JavaScript. The URL property of the document object returns the full path or the page url along with the domain name. Here is the syntax

document.URL

Note that the above line is case sensitive so we can't change the case of the above command.

Let us try to display a sample code to use document.URL property.

<html>
<head>
<title></title>
<script type="text/javascript">
document.write(document.URL);
</script>
</head>
<body>


</body>

</html>

document title of a page by JavaScript

Title tag of the web page is one of the important tag of a html page. The keywords used inside this title tag has high relevant for search engine ranking of a site. You can read on title tag keywords at search engine friendly page here.

You can read html title tag here.

We will discuss here how to manage the title tag by using the document property in JavaScript.

The basic syntax to manage content of the title tag is here.

document.title=" this is my new title ";

document.lastModified of a page

WE can collect the last modified or created date of the current file by using document object lastModified property.


Here is the syntax
 

document.lastModified

The above command is case sensitive

Here is the complete code for you to try.
 

<html>
<head>
<title></title>
<script type="text/javascript">
document.write(document.lastModified);
</script>
</head>
<body>


</body>
</html>


 

document.domain of a page

If we are running a script in a website or in our localhost we can find out the domain name of the website by using document.domain property. Say we have developed a script which can work in multiple domains and we need to find out the present domain name for our processing.

Here is the syntax of our domain property of document object.

document.domain

The output for this page will be

www.plus2net.com

Here is the code.
 

<html>
<head>
<title></title>
<script type="text/javascript">
document.write(document.domain);
</script>
</head>
<body>


</body>
</html>