Practical JS1
- Write and run the Hello World script. Remember to save your files with
the .htm/.html extension.
<html>
<head></head>
<script type ="text/javascript">
document.write("hello World")
</script>
</html>
- Insert an alert box into your code, with a welcome message in. Remember
syntax -alert(“*****”)
- When you close a program quite often you get a dialogue box up to prompt
the user to save their work or not. Below is the code to do this. Write and
run the code. Add your own comments to the code.
<HTML>
<HEAD>
<TITLE>My JavaScript page</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
document.write(“This is my first web page”)
if (confirm("Do you wish to save")) {
alert("You said yes")
}
else {
alert("You said no, all your work is now lost")
}
// End hiding script from old browsers -->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="WHITE">
<NOSCRIPT>
<H2>This page requires JavaScript.</H2>
</NOSCRIPT>
</BODY>
</HTML>
- Write the code below, which detects which browser you are running. Try
running the code in more than one browser to see if it works
<HTML>
<HEAD>
<TITLE>What's your browser?</TITLE>
</HEAD>
<BODY BGCOLOR="WHITE">
<H2>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
if (navigator.appName == "Netscape") {
document.write("You're running a Netscape browser")
}
else {
if (navigator.appName == "Microsoft Internet Explorer") {
document.write("You're running Internet Explorer")
}
else {
document.write("You're not running Netscape or IE--maybe you should?")
}
}
// End hiding script from old browsers -->
</SCRIPT>
</H2>
</BODY>
</HTML>