How to hide javascript code from old browsers that dont run it?
How to hide javascript code from old browsers that dont run it?
Use the below specified style of comments
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");