One page output
1. head file
Copy Code code as follows:
<script language= "JavaScript" >
document.write ("cloud-dwelling Community www.jb51.net");
</script>
2. Within the page
Copy Code code as follows:
<body>
<script>
document.write ("cloud-dwelling community");
</script>
</body>
3. External documents
<script src= "Display.js" ></script>
4. Using the innerHTML of page ID
Copy Code code as follows:
<script>
Window.onload = Writemessage; Call writemessage function when page loads
Writemessage () {
document.getElementById ("HelloMessage"). InnerHTML = "cloud-dwelling community";
Locate the DOM ID (hellomessage) and modify its HTML content
}
</script>
5. Warning
Alert ("Guangzhou Hundred Sinks Logistics Co., Ltd.");
6. Ask
Copy Code code as follows:
if (Confirm ("Visit our homepage")
{
Alert ("Yes, go");
}
else {
Alert ("Exit");
}
7. Input
Copy Code code as follows:
var ans = prompt ("Enter your message", "Hello,");
if (ans) {
Alert ("You say:" + ans);
}
else {
Alert ("Exit, no Message");
}
8. Page Jump
Copy Code code as follows:
<script>
Window.onload = Initall;
function Initall () {
document.getElementById ("redirect"). onclick = Initredirect;
}
function Initredirect ()
{
window.location = "index.html";
return false;
}
</script>
<a href= "http://www.jb51.net" id= "redirect" > cloud-dwelling Community </a>
9. Judgment Branch
Copy Code code as follows:
<script>
Window.onload = Initall;
function Initall () {
document.getElementById ("Lincoln"). onclick = saysomething;
document.getElementById ("Kennedy"). onclick = saysomething;
document.getElementById ("Nixon"). onclick = saysomething;
}
function saysomething () {
Switch (this.id) {
Case "Lincoln":
Alert ("Four score and seven years ago ...");
Break
Case "Kennedy":
Alert ("Ask isn't what your country can do");
Break
Case "Nixon":
Alert ("I am not a crook!");
Break
Default
}
}
</script>
<form action= "#" >
<input type= "button" id= "Lincoln" value= "Lincoln"/>
<input
Type= "button" Id= "Kennedy" value= "Kennedy"/>
<input type= "button" id= "Nixon"
Value= "Nixon"/>
</form>
10. Exception capture
Copy Code code as follows:
Window.onload = Initall;
function Initall () {
var ans = prompt ("input parameter:", "");
try {
if (!ans | | isnan (ANS) | | ans<0) {
throw new Error ("input is not a number");
}
Alert ("square root" + ans + "yes" + math.sqrt (ans));
}
catch (errmsg) {
alert (errmsg.message);
}
}