Beruflich Dokumente
Kultur Dokumente
Ans:
JavaScript Functions
Function Invocation
You will learn a lot more about function invocation later in this
tutorial.
Function Return
Example
12
Why Functions?
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments, to
produce different results.
2. Ans:
Through the use of JavaScript, you can detect when certain events
happen, and cause things to occur in response to those events.
There are two types of event order: event capturing and event
bubbling.
Event capturing starts with the outer most element in the DOM and
works inwards to the HTML element the event took place on and
then out again. For example, a click in a web page would first check
the HTML element for onclick event handlers, then
the body element, and so on, until it reaches the target of the
event.
3.Ans:
Attribute(s) Description
top, left Specifies the position of the top and left edges of an element
bottom, right Specifies the position of the bottom and right edges of an element
width, height Specifies the size of an element
overflow Specifies what to do if an element is bigger than the space allotted for it
This is the default value and specifies that the element is positioned according
to the normal flow of document content (for most Western languages, this is
left to right and top to bottom.) Statically positioned elements are not DHTML
elements and cannot be positioned with the top, left, and other attributes.
To use DHTML positioning techniques with a document element, you must first
set its position attribute to one of the other three values.
absolute
This value allows you to specify the position of an element relative to its
containing element. Absolutely positioned elements are positioned
independently of all other elements and are not part of the flow of statically
positioned elements. An absolutely positioned element is positioned either
relative to the <body> of the document or, if it is nested within another
absolutely positioned element, relative to that element. This is the most
commonly used positioning type for DHTML.
fixed
This value allows you to specify an element's position with respect to the
browser window. Elements with fixed positioning do not scroll with the rest
of the document and thus can be used to achieve frame-like effects. Like
absolutely positioned elements, fixed-position elements are independent of all
others and are not part of the document flow. Fixed positioning is a CSS2
feature and is not supported by fourth-generation browsers. (It is supported in
Netscape 6 and IE 5 for the Macintosh, but it is not supported by IE 5 or IE 6 for
Windows).
relative
4.Ans:
Any value that you use is of a certain type. In JavaScript, there are the
following primitive data types:
Undefinedwhen you try to access a variable that doesn't exist, you get
the special value undefined. The same will happen when you have
declared a variable, but not given it a value yet. JavaScript will initialize it
behind the scenes, with the value undefined.
Nullthis is another special data type that can have only one value, the
null value. It means no value, an empty value, nothing. The difference
with undefined is that if a variable has a value null, it is still defined, it
only happens that its value is nothing. You'll see some examples shortly.
Any value that doesn't belong to one of the five primitive types listed
above is an object. Even null is considered an object, which is a little
awkwardhaving an object (something) that is actually nothing. The
data types in JavaScript the data types are either:
Non-primitive (objects)
If you want to know the data type of a variable or a value, you can use the
special typeof operator. This operator returns a string that represents the
data type. The return values of using typeof can be one of the following
"number", "string", "boolean", "undefined", "object", or "function". In
the next few sections, you'll see typeof in action using examples of each
of the five primitive data types.
Numbers
>>> var n = 1;
>>> typeof n;
"number"
>>> n = 1234;
>>> typeof n;
"number"
The last line in the example above prints the decimal representation of
the octal value. While you may not be very familiar with octal numbers,
you've probably used hexadecimal values to define, for example, colors in
CSS stylesheets.
In CSS, you have several options to define a color, two of them being:
In JavaScript, you put 0x before a hexadecimal value (also called hex for
short).
Exponent Literals
1e1 (can also be written as 1e+1 or 1E1 or 1E+1) represents the number
one with one zero after it, or in other words 10. Similarly, 2e+3 means
the number 2 with 3 zeros after it, or 2000.
>>> 1e1
10
>>> 1e+1
10
>>> 2e+3
2000
>>> typeof 2e+3;
"number"
2e+3 means moving the decimal point 3 digits to the right of the number
2. There's also 2e-3 meaning you move the decimal point 3 digits to the
left of the number 2.
>>> 2e-3
0.002
>>> 123.456E-3
0.123456
>>> typeof 2e-3
"number"
Infinity
>>> Infinity
Infinity
>>> typeof Infinity
"number"
>>> 1e309
Infinity
>>> 1e308
1e+308
>>> var a = 6 / 0;
>>> a
Infinity
Infinity is the biggest number (or rather a little bigger than the biggest),
but how about the smallest? It's infinity with a minus sign in front of it,
minus infinity.
Does this mean you can have something that's exactly twice as big as
Infinityfrom 0 up to infinity and then from 0 down to minus infinity?
Well, this is purely for amusement and there's no practical value to it.
When you sum infinity and minus infinity, you don't get 0, but
something that is called NaN (Not A Number).
Any other arithmetic operation with Infinity as one of the operands will
give you Infinity:
>>> Infinity - 20
Infinity
>>> -Infinity * 3
-Infinity
>>> Infinity / 2
Infinity
>>> Infinity - 99999999999999999
Infinity
NaN
What was this NaN you saw in the example above? It turns out that
despite its name, "Not A Number", NaN is a special value that is also a
number.
You get NaN when you try to perform an operation that assumes
numbers but the operation fails. For example, if you try to multiply 10 by
the character "f", the result is NaN, because "f" is obviously not a valid
operand for a multiplication.
NaN is contagious, so if you have even only one NaN in your arithmetic
operation, the whole result goes down the drain.
>>> 1 + 2 + NaN
NaN
Strings
If you put nothing in quotes, it's still a string (an empty string):
As you saw before, when you use the plus sign with two numbers, this is
the arithmetic operation addition. However, if you use the plus sign on
strings, this is a string concatenation operation and it returns the two
strings glued together.
String Conversions
>>> var n = 1;
>>> typeof n;
"number"
>>> n = "" + n;
"1"
>>> typeof n;
"string"
Operators:
>>> 1 + 2
3
In this code:
+ is the operator
The input values are 1 and 2 (the input values are also called operands)
Instead of using the values 1 and 2 directly in the operation, you can use
variables. You can also use a variable to store the result of the operation,
as the following example demonstrates:
>>> var a = 1;
>>> var b = 2;
>>> a + 1
2
>>> b + 2
4
>>> a + b
3
>>> var c = a + b;
>>> c
3
The following table lists the basic arithmetic operators:
+ Addition >>> 1 + 2
88.99
* Multiplication >>> 2 * 3
/ Division >>> 6 / 4
1.5
>>> 4 % 2
0
>>> 5 % 2
>>> b
123
>>> a
124
>>> b
124
>>> a
124
-- Decrement a Post-decrement
value by 1
>>> var a = 123; var b =
a--;
>>> b
123
>>> a
122
Pre-decrement
>>> b
122
>>> a
122
When you type var a = 1; this is also an operation; it's the simple
assignment operation and = is the simple assignment operator.
>>> var a = 5;
>>> a += 3;
8
>>> a -= 3;
5
Similarly:
>>> a *= 2;
10
>>> a /= 5;
2
>>> a %= 2;
0
5.Ans:
if
if (value == 3)
document.write("Value is three")
else
While
var i = 0
while (i < 3)
{
i++
Do While
days = new
Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Fri
day","Saturday")
var day = 0
do
day++
For
The following example will determine what numeric day of the week
Wednesday falls on.
days = new
Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satu
rday")
var place
{
if (day == days[i])
place = i + 1
Runs one or more commands based on the value of the expression being
evaluated. An example is:
switch (day) {
case 1:
document.write("Sunday<BR>\n")
break
case 2:
document.write("Monday<BR>\n")
break
case 3:
document.write("Tuesday<BR>\n")
break
case 4:
document.write("Wednesday<BR>\n")
break
case 5:
document.write("Thursday<BR>\n")
break
case 6:
document.write("Friday<BR>\n")
break
case 7:
document.write("Saturday<BR>\n")
break
default:
document.write("Invalid Weekday<BR>\n")
break
Break
The break statement may be used to break out of a while, if, switch, dowhile,
or for statement. The break causes program control to fall to the statement
following the iterative statement. The break statement now supports a label
which allows the specification of a location for the program control to jump
to. The following code will print "Wednesday is day 4 in the week".
days = new
Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satu
rday")
var place
place = i + 1
Continue
The continue statement does not terminate the loop statement, but allows
statements below the continue statement to be skipped, while the control
remains with the looping statement. The loop is not exited. The example
below does not include any ratings values below 5.
var sum = 0;
var reviews = 0
sum += ratings[i];
reviews++;
For in Statement
Executes a loop similar to a for statement that executes for all the properties
in an object. The example below lists all properties and their values for the
document object. The document is the HTML page being displayed.
for (i in document)
With Statement
with (document)
{
for (i in this)
6.Ans:
{ this.name=name;
this.add=function(){this.prototype.countInstances++;};
this.add();
};
7.Ans:
function countChar(val) {
var len=val.value.length;
var parts=[];
while(str) {
parts.push(str);
break;
else
parts.push(str.substr(0,partSize));
str = str.substr(pasrSize);
$(#text-character).val(ctext);
$(#text-parts).val(ptext);
$(#text-remaining).val(rtext);
8.Ans:
Javascript Navigator Object:
9.Ans:
JavaScript:
Although there are strong outward similarities between JavaScript and Java,
including language name, syntax, and respective standard libraries, the two
are distinct languages and differ greatly in their design. JavaScript was
influenced by programming languages such as Self and Scheme.
Plugins, such as video players, Adobe Flash, and the wide range
of ActiveX controls enabled by default in Microsoft Internet Explorer, may
also have flaws exploitable via JavaScript (such flaws have been exploited in
the past).
In Windows Vista, Microsoft has attempted to contain the risks of bugs such as
buffer overflows by running the Internet Explorer process with limited
privileges.Google Chrome similarly confines its page renderers to their own
"sandbox".
Web browsers are capable of running JavaScript outside the sandbox, with
the privileges necessary to, for example, create or delete files. Of course, such
privileges aren't meant to be granted to code from the Web.
Incorrectly granting privileges to JavaScript from the Web has played a role in
vulnerabilities in both Internet Explorer and Firefox. In Windows XP Service
Pack 2, Microsoft demoted JScript's privileges in Internet Explorer.
Microsoft Windows allows JavaScript source files on a computer's hard
drive to be launched as general-purpose, non-sandboxed programs
(see: Windows Script Host). This makes JavaScript (like VBScript) a
theoretically viable vector for a Trojan horse, although JavaScript Trojan
horses are uncommon in practice.
10.Ans:
innerText is aware of style and will not return the text of hidden
elements, whereas textContent will.
Using the alert() method of JavaScript, we can easily see these four
properties in action:
1<script type="text/javascript">
6</script>
If the four properties at this point still look like quadruplets to you, that's
ok. The fact of the matter is, 99% of the time, innerHTML is all you'll be
using to create dynamic content.
Armed with the above new information, we can now move forward with
implementing dynamic content in IE. All that's involved is setting the
innerHTML property of a containment element to a new value, effectively
altering what's displayed.
Example:
<script type="text/javascript">
var i=0
function altercontent(){
dcontent.innerHTML=mycontent[i];
i=(i==mycontent.length-1)? 0 : i+1
setTimeout("altercontent()", 3000)
}
window.onload=altercontent
</script>