In JavaScript we only can make three type of Popup Boxes.
These are Alert Box,Confirm box and Prompt Box.
Alert Box: We use Alert boxe in that case where want check the information those given by user.When Alert box popup comes user should press \'ok\' to execute this page.Syntax:alert(\"anytext\");
Confirm Box: Use this box we want to verfy or accept something from user.Syntax:confirm(\"anytext\");
Prompt Box: Use when we want user to input a value before entering a page. When Alert box popup comes user should press \'ok\' to proceed than after entering an input value.syntax:(\"anytext\",\"defaultvalue\")
We can create an Exception by using JavaScript.We used throw with try..catch block.Than we find out the more accurate result message.Syntax:throw(exception) This exception may be a string, integer, Boolean or an object.
We know that methods are the actions that we performed on the objects. Here,I use a toLowerCase() method to change the text of a string object into the lowercase letters.<script type=\"text/javascript\"> var str=\"Hello world!\"; document.write(str.toLowerCase()); </script>Output:hello world!
JavaScript provide us facility to add some special characters (like:new line,quotes and some other special character) to a text string. We use this special character after backslash sign.
Example:
var txt = \"Hello Client,\"thank you for visit\"R4R Welcomes You!\";
document.write(txt);
We know that in JavaScript we write string inside the double or single quote. That say we sliced string to: Hello client. We used special character \'/\' to solved this problem. We placed \'/\' before each double quote in\"thank you for visit\".
var txt = \"Hello Client,\"thank you for visit\"R4R Welcomes You!\";
document.write(txt); After that we will get our required output.Like that Hello Client,thank you for visit R4R Welcomes You!
I have given you some special characters that we used in JavaScript are:\'single quote\"double quote&ersand\\backslashnnew linercarriage returnttabbbackspacefform feed
Using Boolean Object we can get output in the form of true/false(Boolean Values).
Syntax used to create Boolean Object are given below:var myBoolean=new Boolean();
If the Boolean object has no initial value or if it is 0, -0, null, \"\", false, undefined, or NaN, the object is set to false. Otherwise it is set to true.
Below I have given you code to create Boolean objects with an initial value of false.
var myBoolean=new Boolean(); var myBoolean=new Boolean(0); var myBoolean=new Boolean(null); var myBoolean=new Boolean(\"\"); var myBoolean=new Boolean(false); var myBoolean=new Boolean(NaN);
Below I have given you code to create Boolean objects with an initial value of true.
Using JavaScript we can validate the input of the HTML page before sending it into server. I have given you example that shows you how perform on required fields and E-mail. we use this code when we want to validate fields using JavaScript.
Syntax:
function validate_required(field,alerttxt)
{
if (value==null||value==\\\"\\\")
{
alert(alerttxt);
return false;
}
else
{
return true;
}
}
We use that code when we want to perform E-mail validation.
I have given you Example which show you how to call function in JavaScript.
Example
<script language=\"javascript\" type=\"text/javascript\"> //Here I Declare Function function fun() { alert(\"function called\"); } //Here I call function from the script fun(); </script> //call function is called when click on button. <input type=\"button\" onclick=\"fun()\"/>
JavaScript is an scripting language for the client side web development means web pages. It is designed by Brenden Eich comes in 1995.
Developer of JavaScripts are Netscape Communications Corporation, Mozilla Foundation. As name indicate you think it is essential related to Java. Its wrong its syntax matched with C Syntax,it use some java name and their convension. It is influenced by Seff,C,Scheme,Perl,Python,Java. Using this we can do many thimgs like: to improve Design,validate forms,detect browsers etc. JavaScript can be use by new programmer easily. I have given you some most important point related to JavaScript.
JavaScript is a trademark of Sun Microsystem.
It is dynamic,Weak and prototype language.
Using JavaScript we can make our HTML pages interactive.
JavaScript is a scripting language.
We can embedded JavaScript directly into HTML pages.
It is an interpreted language means for execution their is no need to pre-compilation of JavaScript. Latest version of JavaScript is 1.8/ 2008
we can define JavaScript as a sequence of JavaScript Statements.Statement write in JavaScript Code executed one by one in order they have written. Example:
<script type=\"text/javascript\">
document.write(\"<h1>Here you write header</h1>\");
document.write(\"<p>Here write your first paragraph</p>\");
document.write(\"<p>Here write your second paragraph</p>\");
Java (developed by Sun Microsystems) is a powerful and much more complex programming language in the same category as C and C++.
JavaScriptwas created by Brendan Eich at Netscape and was first introduced in December 1995 under the name of LiveScript. However, it was rather quickly renamed JavaScript, although JavaScripts official name is ECMAScript, which is developed and maintained by theECMA (European Computer Manufacturer\'s Association) International organization.
ECMA Organization developed and maintain ECMAScript.
An official JavaScript standard is ECMA-262.Development to of this started in 1996 and It is first used in ECMA General Assembly in june 1996.
It gets cettificated by international ISO (ISO/IEC 16262) standard in 1998.
It is usually based on JavaScript and Jscript Repectively given by Netscape and Microsoft.First it is invented by Brendan Eich at Netscape and applied on Navigator 2.0 after 1996 it is used on all type of Netscape and micrisoft browser.
We use JavaScript in HTml page inside the
In the above example we write JavaScript inside the
Those we write inside the document.write it will print as output.
Output:
R4R Welcomes you on that place where professtionals meet.
When you use JavaScript in HTML pages.But keep this in mind bwoser that we used in Html doesn't support to show javaScript as page content.We used HTML comment tage to hide the JavaScript. We start comment tag() after the last JavaScript line.//is the JavaScript comment symbol.
Example:
Hello World!
When we write javaScripts inside the body that will execute when page load.If we write JavaScripts inside the head than it willexecuted when we called. When we put JavaScript with in head of HTML.Than JavaScript is executed when we called them.
Example:
/*Their is no limit to put a fixed number of JavaScripts on your page.You can use JavaScripts in both head and body together.*/<html><head><script type=\"text/javascript\">........</script></head><body><script type=\"text/javascript\">........</script></body>
We use external JavaScript when we want to execute same JavaScripts on many pages.When we use external JavaScript using this their is no need to write JavaScripts on every page.
If we want to use JavaScript than we first write JavaScript in enternal file than save this external JavaScript file by using .js file extension.
External Script doesn\'t contain
JavaScript is an Case-Sensitive scripting language. It means keep your eyes open when you write JavaScript Statement,create or call variables,functions and objects.
JavaScripts called as a sequence of Statement those are executed on browser.We use javaScript Statement to say the browser what to write.
Example:
document.write("R4R Welcomes You!");
Using above Statement we say browser to write R4R Welcomes You! on browser.We distinguish each javaScript Statement by semicolon(;).
We can grouped JavaScript Statements into Blocks.We use Blocks when we want that some Statement will execute together.We open Blocks with left curly bracket'{' and ends with write curly bracket'}'.
Example:
Generally, We use Comments in JavaScript to make JavaScipts easily understand.We start to write single line comment by //.Example:<script type=\"text/javascript\">// Here write the header:document.write(\"<h1>This is my header</h1>\");// Below I have write two paragraphs:document.write(\"<p>This is my first paragraph</p>\");document.write(\"<p>This is my second paragraph </p>\");</script> We can write multiline comment on JavaScript also.We start to write multi line comment with /* and ends with */.Example:<script type=\"text/javascript\">/*Here I have write one header and two paragraphs*/document.write(\"<h1>This is my header</h1>\");document.write(\"<p>This is my first paragraph </p>\");document.write(\"<p>This is my second paragraph </p>\");</script>
We can stop execution of some code by using comments like that,
//document.write("<p>This is my first paragraph.</p>");
We can also write comments on the end of the line.
document.write("Hello");//This statement will write Hello.
document.write("World");//This statement will write World.
Variables may be of single character or group of characters. I have given you rules using them you can use variables in JavaScript.
1.In JavaScript variables are case-sensitive means xand X both are different variables in case of JavaScript. 2.Keep in mind JavaScript should begin with a letter or underscore\'_\'.
Below I have given how to create variables and assign values in Javascript.
Example:var v;var variablename; Here,I have only declare(created) variables not assigning any values to them. var v=7;var variablename=\"xyz\"; Here, I have assign the values to both variables are 7 and xyz respectively.
We assign number without quotes and if want to assign text to variables than write text with in quotes.
InJavaScript When you assign the values to the variables without declaring them.javaScript automatically declare the variable.Example: Here,we assign the value to v and variablename with declaring them.v=7;variablename=\"xyz\"; Than it will work as same as,var v=7;var variablename=\"xyz\";
JavaScript give us facility to Redeclare JavaScript Variable without loosing data that variables have already contain. we can also perform arithmetic operations with JavaScript variables. a=9;b=a-7;
Using Camparison and Logical Operator we test and gives result in the form of true or false
.I have given some comparison operators with their describtion.== is equal to
=== is exactly equal to
!= is not equal to
> is greater than
is less than
>= is greater than or equal to
<= is less than or equal to
Example:
if (gender==M) document.write(\"he is a male\");
Logical Operator: We use logical operators to find out the logic between variables or values.Let, x=6 and y=3, logical operators: && and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true
Conditional Operator: Using conditional operater we can assign the resultant value to the variable.Syntax:variablename=(condition)?val1:val2
Example:welcome=(user==\"Professional\")?\"Dear Professional \":\"Dear \"; If the user is equal to professional then Dear Professional is assign to the welcome otherwise Dear assign to the welcome.
Using Conditional statements we can apply different actions on different conditions.
JavaScript provide has four different conditional statements.
1.if statement: This statement is used to execute some specific code when specific condition is true.
Syntax:
if (condition)
{
this code will executed if condition is true
}
Example:
<script type="text/javascript">
//Write a "Good Afternoon" welcome if
//the time is greater than 12
var d=new Date();
var time=d.getHours();
if (time>12)
{
document.write("<b>Good Afternoon</b>");
}
</script>
2.if...else statement: If condition is true than execute some code else execute different code.
Syntax:
if (condition)
{
This code will executed if condition is true
}
else
{
This code will executed if condition is not true
}
Example:
<script type="text/javascript">
//If the time is greater than 12,
//you will get a "Good Afternoon" welcome.
//Otherwise you will get a "Good day" welcome.
var d = new Date();
var time = d.getHours();
if (time > 12)
{
document.write("Good Afternoon");
}
else
{
document.write("Good day");
}
</script>
3.if...else if....else statement: Using this statement we can select one of many blocks of code to be executed.
Syntax:
if (condition1)
{
This code will executed if condition1 is true
}
else if (condition2)
{
This code will executed if condition2 is true
}
else
{
This code will executed if condition1 and
condition2 are not true
}
Example:
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good Afternoon</b>");
}
else
{
document.write("<b>R4R Welcomes You!</b>");
}
</script>
4.switch statement: Using this statement we can select one of many blocks of code to be executed.
Syntax;
switch(i)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is
different from case 1 and 2
}
Example:
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6.
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 0:
document.write("It is Sunday");
break;
case 1:
document.write("It is Monday");
break;
case 2:
document.write("It is Tuesday");
break;
case 3:
document.write("It is Wednesday");
break;
case 4:
document.write("It is Thursday");
break;
case 5:
document.write("It is Friday");
break;
case 6:
document.write("It is Saturday");
break;
default:
document.write("Press only numbers from 0 to 6 ");
}
</script>
In JavaScript we only can make three type of Popup Boxes.
These are Alert Box,Confirm box and Prompt Box.
Alert Box: We use Alert boxe in that case where want check the information those given by user.When Alert box popup comes user should press \'ok\' to execute this page.Syntax:alert(\"anytext\");
Confirm Box: Use this box we want to verfy or accept something from user.Syntax:confirm(\"anytext\");
Prompt Box: Use when we want user to input a value before entering a page. When Alert box popup comes user should press \'ok\' to proceed than after entering an input value.syntax:(\"anytext\",\"defaultvalue\")
Functions is executed by the event or whenwe called.Functions are reusable block of code.
If we want that the browser from executing a script when the page loads,Than we have to write our script into a function.
We can execute the function code by using an event or simply called the function.We can call from any where of the page.If you want to call the function from different page than you have embedded function with in external JavaScript.
We can defined as function in both <head> and <body>.
Example:
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello User!");
}
</script>
</head>
<body>
<form><input type="hidden" name="phpMyAdmin" value="f43d4e0b88acea2d2a393515f6bf38f2" /><input type="hidden" name="phpMyAdmin" value="70ac9566533a2665b6597346aab7f985" />
<input type="button" value="Click me!"
onclick="displaymessage()" >
</form>
</body>
</html>
We function in JavaScript like that,
Syntax:
function functionname(var1,var2,...,varz)
{
Write some code here
}
Using var1, var2,..varz variables or values passed into the function.Where as { and } show that the start and end of function.
Lifetime of JavaScript variables is total time when variable is created till this variable will destroy.
When we declare any varaible inside the function than variable Lifetime will start when it declare and end with when this function will executed. Means that when function is exit than variable declare on this function will destroyed.We called this type of variable local variable.
we can use same local variable with different function.may be same local variable have different values in different functions.because this variables are declared in function than the will destroy when function will executed. If you want that all function will use the same variablename with same value.Than we can declare this variables outside the functions. Lifetime of this type of variable is started when they are created and destroy when page will will closed.
Using Loop we can execute same block of code into a specific number of times.
JavaScript support two ypes of Loops:
1.for
2.while
1.for: We use for loop to execute a block of code for specfic number of times.
Syntax:
for (variable=startvalue;variable<=endvalue;variable=variable+increment)
{
code executed
}
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("Number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
Output:
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
2.while: This loop execute block of code when specifc condition is true.
Syntax:
while (variable<=endvalue)
{
code executed
}
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
{
document.write("Number is " + i);
document.write("<br />");
i=i+1;
}
</script>
</body>
</html>
Output:
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
3.do..while: In do..while Loop executed atleast one time after that it check the condition if it true than execution proceed otherwise loop stopped the execution.
Syntax:
do
{
code executed
}
while (variable<=endvalue);
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
do
{
document.write("Number is " + i);
document.write("<br />");
i=i+1;
}
while (i<0);
</script>
</body>
</html>
Output:
Number is 0
Break and Continue are Special Statement which we used inside the JavaScript.
1.Break: If we write break statement inside the loop than it will break that loop and continue execution of code(if any) that we write after this loop.
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
if (i==2)
{
break;
}
document.write("Number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
Output:
Number is 0
Number is 1
2.Continue: If we use continue statement than it will break the current loop and continue with the next value.
Example:
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=5;i++)
{
if (i==2)
{
continue;
}
document.write("Number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
Output:
Number is 0
Number is 1
Number is 3
Number is 4
Number is 5
We can check the error from web pages by only two ways.
These are:
1.try..catch
2.onerror try..catch: Using try..catch we can check the errors from a block of code. In this try block have a code that want to run and the catch block have a code to be executed if any error comes.
Syntax:try{//code want to execute}catch(err){//if error comes than this code will handle}
Since alert() is misspelled than JavaScript error comes.This time, the catch block catches the error and executes a code to handle this error.Than displays a error message.
Using Script Object we can to stored text.
Example:
Here, I used the String Object to find out the length of the string.
var txt="Hello world!";
document.write(txt.length);
Output:
12
RegExp is stands for regular expression.Using RegExp Object we can perform some specific search in text. We use them when we want search for some specific Pattern in text. Suppose if want to search a single character from given pattern than write code like that:var pattern=new RegExp(\"[abc]\"); Find any character between the brackets.
RegExp Object has 3 methods:
test()
exec()
compile().
Using test() we can search a specific value from the string.Return result in terms of true or false.
Using exec() we can search a specific value from the string.Return value if it found the value otherwise it returns null if does not find the searched value.
test() :When we want to search a specific value from a string,It gives output in the forms of True or False.
Example:
var pattern=new RegExp(\"t\");
document.write(pattern.test(\"It will never be wasted\"));
Output: true
exec():It is also used to search a value from a string but it generate different result. like: If vlue is matched return value otherwise I will return NULL(means value does not matched).
Example:
var pattern=new RegExp(\"t\");document.write(pattern.exec(\"Always try to improve\"));
Output:t
compile() : Using compile() method we can change both the search pattern and add or remove the second parameter.Example:var pattern=new RegExp(\"t\");document.write(pattern.test(\"Always try to improve \"));pattern.compile(\"z\");document.write(pattern.test(\"Always try to improve\"));0utput :truefalseReason of this output is that first it check for pattern \'t\' because it is in our text.So,it will return true and than search for \'z\'.Because it is not in our text than it will return false.
Client Side JavaScript is that in this we can use the core language plus extras such as the predefined objects, only relevant to running Javasript in a browser. Client Side JavaScript is embedded directly in the HTML pages and is interpreted by the browser completely at the run time.
Server Side JavaScript is that in this we can use the core language plus extras as the predefined objects and functions only relevant to running Javasript in a server. Server Side JavaScripts are compiled before they are deployed.
JavaScript is a platform-independent,event-driven, interpreted client-side scripting and programming language developed by Netscape Communications Corp. and Sun Microsystems.
I/O operations like reading or writing a file is not possible with client-side javascript. However , this can be done by coding a Java applet
that reads files for the script.
This depends on the user's browser and OS.
In the case of Netscape with Windows OS, all the cookies are stored in a single file called cookies.txt
c:\Program Files\Netscape\Users\username\cookies.txt
In the case of IE,each cookie is stored in a separate file namely username@website.txt.
c:\Windows\Cookies\username@Website.txt
Generation of HTML pages on-the-fly without accessing the Web server. The user can be given control over the browser like User input validation Simple computations can be performed on the client's machine The user's browser, OS, screen size, etc. can be detected Date and Time Handling
The "Access Denied" error in any browser is due to the following reason.
A javascript in one window or frame is tries to access another window or frame whose document's domain is different from the document containing the script
Yes.
Since javascript is a client-side script it does require the web server's help for its computation,so it is always faster than any server-side script like ASP,PHP,JSP etc.
No.
Java and javascript are two different languages.
Java is a powerful object - oriented programming language like C++,C whereas Javascript is a client-side scripting language with some limitations.
Breaking is possible within a string statement by using a backslash \ at the end but not within any other javascript
statement.that is ,
document.write("Hello \
world");
is possible but not
document.write \
("hello world");