JavaScript Basics first chapter JavaScript and user-side _ Basics

Source: Internet
Author: User
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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.