Write the first JavaScript code, using the web as an example

 If you have written CSS syntax, it should be easy to write JavaScript, because the configuration is similar, but this is not the point. The following article will show you a simple JavaScript example in the form of a web page. There are three ways to write JavaScript. A simple step, the first step is to declare, that is, tell the browser that the code you wrote belongs to JavaScript syntax, so that the browser will execute it correctly, instead of displaying the code you wrote as a normal string , Of course, the second step is to write the code! The third step is testing. When writing this type of script, you usually write and test as you write it. If you have any problems, you should immediately modify it to avoid writing a long list of errors before you start debugging.


Now start to write the first JavaScript code. Please open Notepad or Notepade++ code editor , copy the following code to your Notepad or editor, and save the new file as test.html, and finally Open the test.html file test with a browser.
<html>
 <body>
 <script type="text/JavaScript">
 <!--
 document.write("Welcome")
 //-->
 </script>
 </body>
</html>
If everything goes well, you should be able to see the text "Welcome" on the web page. If there are garbled codes, it means there is a problem with the side code of the web page. Please change the words "Welcome" in the code to English letters or numbers, and test again It should be able to display the content you wrote normally. The coding problem is a little more complicated and requires additional space to introduce it. I won't write more here, just focus on the understanding of this example!

The HTML part of this code is relatively simple (if you don’t understand HTML syntax, please refer to this article: Writing the first HTML page example ). The point is that the difference between <script type="text/JavaScript"> and </script> It is used to put your JavaScript grammar. When the browser sees the declaration of this Script tag, it knows to execute the JavaScript grammar. If you do not have such a declaration, JavaScript may not execute correctly!

Post a Comment

0 Comments