In the last lecture, we learned where JavaScript appears and what it looks like. Now we start learning this language. In this lesson we will learn how JavaScript stores information, how to make decisions based on information, and how to alternate images according to user interaction requirements
Are you ready? Start learning the basics of computer programming now. Lecture 1, variables.
If you have studied algebra, you must have seen variables. It doesn’t matter if you haven’t learned it before. Variables are a simple way for JavaScript to store information. For example, when you write: "x=2," "x" is a variable that stores the value 2. If you then say "y=x 3,", "y" will have the value "5"
Here is a JavaScript example using variables.
In this example, we browse the source code step by step. You will see:
There is nothing new here, it is the end of a JavaScript piece.
This is all the JavaScript in the header file in this example. When JavaScript executes these codes, the above variables will be defined. But at this point these variables haven't done anything yet, which is what is done in the body of the example.
Now that we have the variable defined, let’s do something with it.
When you've finished that work, it's time to start practicing if clauses.
The application of "if" clause can make the program respond differently according to the value input by the user. For example, you could write a program so that it reacts differently to you than to other people. Here is its basic format:
if (some condition is true)
{
do something;
do something;
do something;
If the condition is true, execute the statement in the curly brackets.
Remember: whitespace is the only thing that keeps your program readable. Of course you could write the entire if statement in one line, but it would be difficult to read.
Here is an example of an if clause.
Once the user clicks on a link or moves the mouse over it, JavaScript sends a link event. A link event called onClick is sent when the user clicks on it. The other is called onMouseOver, which is sent when the user moves the mouse over it.
You can use these events to affect what the user sees.
The first interesting thing is that there is no <script> tag. This is because anything that appears within onClick and onMouseOver quotes is understood by JavaScript. In fact, the quotes before the end of the sentence allow you to write JavaScript as a single line. You can put the entire JavaScript program within an onClick quote, but it will look ugly. <br/><br/> Please look at the first line: <br/><br/> <a href="#" onClick="alert('Ooo, do it again!');">Click on me!< /a> <br/><br/> This is like a formal targeting tag, but it has the magic onClick="" which says "When someone clicks on the link, run the JavaScript in the quotes" Note after the alert There is a semicolon. <br/><br/> Please also note that there is nothing in the quotation marks of href="", which indicates that although the link is there, it cannot go there when you click it. <br/><br/> The next line is: <br/><br/> <a href="#" onMouseOver="alert('Hee hee!');">Mouse over me! <br/><br/> This is just like the first line, except onMouseOver is used instead of onClick. <br/><br/> Now that we have finished learning link events, please enter the wonderful picture alternation! <br/><br/>One of the main applications of JavaScripts is its function of automatically switching images when you move the mouse. <br/><br/> Here is a quick and basic image exchange example. <br/><br/> <img src="/static/imghwm/default1.png" data-src="button_r.gif" class="lazy" name="the_image" alt="JavaScript Beginner Tutorial (Lesson 2)_Basic Knowledge" > <br/> <a href="#" onMouseOver="document.the_image.src='button_d.gif';" >change <br/><br/> Let’s analyze this example step by step, <br/><br/> The first line is: <br/><br/> <img src="/static/imghwm/default1.png" data-src="button_r.gif" class="lazy" name ="the_image" alt="JavaScript Beginner Tutorial (Lesson 2)_Basic Knowledge" > <br/><br/> This is like a standard <img src="/static/imghwm/default1.png" data-src="newhome.gif" class="lazy" src= alt="JavaScript Beginner Tutorial (Lesson 2)_Basic Knowledge" > tag, except that it is given a name: the_image, and the name is arbitrary: my_image, a_box.... ..but no spaces are allowed inside. <br/><br/> The next line is: <br/><br/> <a href="#" onMouseOver="document.the_image.;">change <br/> <br/> This is where the picture exchange happens. Just like you saw onMouseOver before. <br/> The main JavaScript that appears in onMouseOver quotes is: <br/><br/> document.the_image.; <br/><br/> This sentence says: "Find the name 'the_image' image and change its src to button_d.gif." Note that the entire statement uses double quotes, while 'button_d.gif' uses single quotes. Although the two are interchangeable, be careful not to get confused if one set of quotation marks exists within another. You can write " 'something' " or ' "something" ' , but you can't write: " 'something" ' or " "something" ". Got it? <br/><br/>Just as I didn’t tell you many details of how document.writeln() works, I didn’t tell you how image exchange works in this example. You will learn more about "Goal-Oriented Programming" and "File Object Modules" in the next lecture. <br/><br/> Please note! The image to be exchanged must be the same size as the original image! Otherwise, it deforms. <br/><br/> Below are two pieces of code. (Note: Take a look if you are interested, there is no explanation here, you can skip) <br/><br/> <script language="JavaScript"> <br/> <!-- hide me <br/><br/> var temp = ""; <br/> var image1 = 'netteach.gif'; <br/> var image2 = 'phtshop1.gif'; <br/> var image3 = 'newhome.gif' <br/><br/> var user_name = prompt ("What's your name", ""); <br/> if (user_name == "") <br/> { <br/> user_name = "stranger"; <br/> } <br/> document.write(user_name); <br/> // end hide --> <br/> </script>
Now let’s review what we learned today.
Variable
Variable value can be a number or a string. There are some restrictions and rules to remember when naming variables.
Statement
Statement ends with semi-circular brackets.
String
A string is a sequence of actions in quotation marks. The quotation marks can be single quotation marks or double quotation marks. There are many wonderful things you can do with strings. You can use "+" to concatenate two strings.
document.writeln()
Document.writeln() can be used to write text and HTML in web pages.
prompt
You can use prompt to get user input feedback.
if--else
You can use the if--else clause to make your JavaScript behave differently according to different user responses.
Link events
OnClick and onMouseOver in an href can run JavaScript based on user response.
Image transformation
After the image is named, JavaScript can be used to change the displayed image.
If you feel that you have mastered everything we have said above, then congratulations!
There are still many things to learn. Next time, we'll get to the heart of JavaScript: the Document Object Model. We'll also learn how to open and manipulate windows and frames, and start building our own new browser.

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
