Lec #2 Js

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

DCIT 65 Web Development Lecture

#2

The <script> tag

<script> </script> - used to assist the browser in recognizing lines of code in an HTML document
as belonging to a script.

<script> tag attribute


type - advises the browser to treat the code within the tag as JavaScript.
<script type=text/javascript>
one or more lines of JavaScript code here
</script>

language
- used to be the way to specify the scripting language for the enclosed code.
- allowed scripters to specify the language version.

For example, if the scripts included code that required JavaScript syntax available only in
version 4 browsers (which implemented JavaScript version 1.2), the script> tag used to be
written as follows.

<script language=JavaScript1.2> </script>


<script language=vbscript>

src
- works with more recent browsers to blend the content of an external script file into
the current document.
- points to the file containing the script code.
- such files must end with a .js extension.

Example
<script type=text/javascript src=myscript.js> </script>
Tag positions

script in the head


<html>
<head>
<title>A document </title>
<script type=text/javascript>
// script statement(s) here

</script>
</head>
<body>
</body>
</html>

script in the body


<html>
<head>

1 Prepared by: Ms. Steffanie D. Maglasang


DCIT 65 Web Development Lecture
#2

<title>A document </title>


</head>
<body>
<script type=text/javascript>
// script statement(s) here

</script>
</body></html>

scripts in the head and body


<html>
<head>
<title>A document </title>
<script type=text/javascript>
// script statement(s) here

</script>
</head>
<body>
<script type=text/javascript>
// script statement(s) here

</script>
</body></html>

two scripts in the head or in the body


<html>
<head>
<title>A document </title>
<script type=text/javascript src=js1.js> </script>
<script type=text/javascript>
// script statement(s) here

</script>
</head>
<body>
</body>
</html>

hiding scripts from most old browsers


<script type=text/javascript>
<!--
// script statement(s) here

-->
</script>

document.write( )
2 Prepared by: Ms. Steffanie D. Maglasang
DCIT 65 Web Development Lecture
#2

- a javascript function that is used to write output in your page.

Example

<script type=text/javascript>
document.write(hello world);
</script>

Data Type Conversions

Examples
3+3 // result = 6

3 + 3 // result = 33

3 + 3 + 3 // result = 63

Converting strings to numbers

Examples
parseInt(42) // result = 42

parseInt(42.33) // result = 42

parseFloat(42) // result = 42

parseFloat(42.33) // result = 42.33

3 + 3 + parseInt(3) // result = 9

Converting numbers to strings

Examples
( + 2500) // result = 2500
( + 2500.length) // result = 4

3 Prepared by: Ms. Steffanie D. Maglasang


DCIT 65 Web Development Lecture
#2

Variable
- container for information you want to store.

2 types of variables
local variables are variables defined inside functions with the var keyword
global variables are variables defined outside functions.

Global and Local Variable Scope Demonstration

<html>
<head>
<script type="text/javascript">
var aBoy="Pepito Manaloto"; // global
var hisDog="Snoopy"; // global
function jonjie()
{
var hisDog="Boski";
var ou=hisDog + " does not belong to a " + aBoy + ".<br>";
document.write(ou);
document.write(hisDog + " does not belong to a " + aBoy + ".<br>");
}
</script>
</head>
<body>
<script type="text/javascript">
jonjie();
document.write(hisDog + " belongs to " + aBoy + ".");
</script>
</body>
</html>

4 Prepared by: Ms. Steffanie D. Maglasang

You might also like