Basic JavaScript tutorials and basic javascript tutorials

Source: Internet
Author: User

Basic JavaScript tutorials and basic javascript tutorials
1JavaScript is nested in html

The common practice is to put the function in the

The following is a javascript code that verifies whether the input is empty. You can create an html file in webroot for testing.

<! DOCTYPE html>

<Html>

<Body>

 

<H1> my first JavaScript

 

<P> enter a number. If the input value is not a number, a prompt box is displayed in the browser. </P>

 

<Input id = "demo" type = "text">

 

<Script>

Function myFunction ()

{

Varx = document. getElementById ("demo"). value;

If (x = "" | isNaN (x ))

{

Alert ("NotNumeric ");

}

}

</Script>

 

<Button type = "button" onclick = "myFunction ()"> click here </button>

 

</Body>

</Html>

 

Of course, no function is required.

 

<! DOCTYPE html>

<Html>

<Body>

 

<H1> My Web Page

 

<P id = "demo"> A Paragraph. </p>

 

<Div id = "myDIV"> a div. </div>

 

<Script>

Document. getElementById ("demo"). innerHTML = "HelloWorld ";

Document. getElementById ("myDIV"). innerHTML = "Howare you? ";

</Script>

 

</Body>

</Html>

2JavaScript is independent of html files. The file type is *. js.

<! DOCTYPE html>

<Html>

<Body>

<Scriptsrc = "myScript. js"> </script>

</Body>

</Html>

MyScript. js and jsp files or files are located in the same location, that is, all under webroot. Of course, you can also create a new folder under webroot.

3. Introduction to common functions for writing 4js files to external js files 4.1 getting html Element Content
<script>
document.getElementById("demo").innerHTML="My First JavaScript";
</script>
4.2 output content to the interface
<script>
document.write("<p>My First JavaScript</p>");
</script>
4.3 write comments

Just like Java, a single line of "//" can be used, multiple lines of "/**/"

4.4 Variables

The int and string types are not described as in Java. All var.

Including the object type.

person=new Object();
person.firstname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";
4.5 Functions

In the same way, functions and Java functions can contain parameters, values, or values.

A function can call the return value of another function.

In an html file

<Button onclick = "myFunction ('Harry Potter ', 'wizard')"> click here </button>
<Button onclick = "myFunction ('bob', 'builder')"> click here </button>

In the js File

<Button onclick = "myFunction ('bill Gates', 'ceos ')"> click here </button>
 
<script>
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script>

The following is an example of mutual function calls.

function myFunction()
{
var x=5;
return x;
}

In another function

var myVar=myFunction();

Of course, the value of myVar is 5.

4.6 exit the Function

 

function myFunction(a,b)
{
if (a>b)
  {
  return;
  }
x=a+b
}

When a> B exits the Function

 

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.