Professional Documents
Culture Documents
Lec #2 Js
Lec #2 Js
Lec #2 Js
#2
<script> </script> - used to assist the browser in recognizing lines of code in an HTML document
as belonging to a 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.
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
document.write( )
2 Prepared by: Ms. Steffanie D. Maglasang
DCIT 65 Web Development Lecture
#2
Example
<script type=text/javascript>
document.write(hello world);
</script>
Examples
3+3 // result = 6
3 + 3 // result = 33
3 + 3 + 3 // result = 63
Examples
parseInt(42) // result = 42
parseInt(42.33) // result = 42
parseFloat(42) // result = 42
3 + 3 + parseInt(3) // result = 9
Examples
( + 2500) // result = 2500
( + 2500.length) // result = 4
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.
<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>