JavaScriptForBeginners(转载)_基础知识
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScriptForBeginners(转载)_基础知识,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11350字,纯文字阅读大概需要17分钟。
内容图文
![JavaScriptForBeginners(转载)_基础知识](/upload/InfoBanner/zyjiaocheng/307/3d0f7d97a8d84df18d5ce9124814fcd2.jpg)
Contents
Embedding and including
write and writeln
Document object
Message box
Function
Event handler
Form
Link
Date
Window
Frame
Embedding and including
Let's first see a simple example:
< head >
< title > This is a JavaScript example </ title >
< script language ="JavaScript" >
</ script >
</ head >
< body > Hi, man! </ body >
</ html >
Usually, JavaScript code starts with the tag and ends with the tag script>. The code placed between and . Sometimes, people embed the code in the tags:
< head > head >
< body >
< script >
![](http://www.blogjava.net/Images/dot.gif)
script >
body >
html >
Why do we place JavaScript code inside comment fields
?
It's for ensuring that the Script is not displayed by old browsers that do not support JavaScript. This is optional, but considered good practice. The LANGUAGE attribute also is optional, but recommended. You may specify a particular version of JavaScript:
You can use another attribute SRC to include an external file containing JavaScript code:
For example, shown below is the code of the external file hello.js :
The external file is simply a text file containing JavaScript code with the file name extension ".js".
Note:
- Including an external file only functions reliably across platforms n the version 4 browsers.
- The code can't include tags and script>, or you will get an error message.
write and writeln
In order to output text in JavaScript you must use write() or writeln(). Here's an example:
< HEAD >
< TITLE > Welcome to my site TITLE > HEAD >
< BODY >
< SCRIPT LANGUAGE ="JAVASCRIPT" >
SCRIPT >
BODY >
HTML >
Note: the document object write is in lowercase as JavaScript is case sensitive. The difference between write and writeln is: write just outputs a text, writeln outputs the text and a line break.
Document object
The document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code:
In this code, document is the object. write is the method of this object. Let's have a look at some of the other methods that the document object possesses.
lastModified
You can always include the last update date on your page by using the following code:document.write( " This page created by John N. Last update: " + document.lastModified);
script >
All you need to do here is use the lastModified property of the document. Notice that we used
+
to put together This page created by John N. Last update: and document.write.
bgColor and fgColor
Lets try playing around with bgColor and fgColor:document.bgColor = " black "
document.fgColor = " #336699 "
script >
Message Box
alert
There are three message boxes: alert, confirm, and prompt. Let's look at the first one:< script >
window.alert( " Welcome to my site! " )
script >
body >
You can put whatever you want inside the quotation marks.
confirm
An example for confirm box:prompt
Prompt box is used to allow a user to enter something according the promotion:In all our examples above, we wrote the box methods as window.alert(). Actually, we could simply write the following instead as:
confirm()
prompt()
Variables and Conditions
Let's see an example:
var x = window.confirm( " Are you sure you want to quit " )
if (x)
window.alert( " Thank you. " )
else
window.alert( " Good choice. " )
script >
There are several concepts that we should know. First of all, var x = is a variable declaration. If you want to create a variable, you must declare the variable using the var statement. x will get the result, namely, true or false . Then we use a condition statement if else to give the script the ability to choose between two paths, depending on this result (condition for the following action). If the result is true (the user clicked "ok"), "Thank you" appears in the window box. If the result is false (the user clicked "cancel"), "Good choice" appears in the window box instead. So we can make more complex boxes using var, if and those basic methods.
var y = window.prompt( " please enter your name " )
window.alert(y)
script >
Another example:
< script >
var x = confirm( " Are you sure you want to quit? " )
if ( ! x)
window.location = " http://www.yahoo.com "
script >
head >
< body >
Welcome to my website!.
body > html >
If you click "cancel", it will take you to yahoo, and clicking ok will continue with the loading of the current page "Welcome to my website!". Note: if(!x) means: if click "cancel". In JavaScript, the exclamation mark !means: "none".
Function
Functions are chunks of code.Let's create a simple function:
{
document.write("Hello can you see me?")
}
Note that if only this were within your
内容总结
以上是互联网集市为您收集整理的JavaScriptForBeginners(转载)_基础知识全部内容,希望文章能够帮你解决JavaScriptForBeginners(转载)_基础知识所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。