How to use javascript in HTML?
Use<script> input html.
Scriptdefine 6 property(属性)
async:forexpand script file download right now。donot distrub other operation。
Charset:definesrc character set。likeutf-8,gbk。
defer:usingscript when file reloaded。
language:nouse
src:expandfile for use code
type:replacelanguage function。
Twoways to use <script>
1.inhtml use javescript code
eg:
<scripttype=“text/javascript”>
functionsayhi()
{alert(“hi”);}
</script>
ifexplorer contains somelike </script> word.we should Escape(转义)</script> as <\script>
2.concludeexternal(外部)javascript file.
Eg:<scripttype=“text/javascript” src=“example.js”></script>
banfor html file
Labellocation
Intradition ways.all <script> element(元素)shouldbe in html<head>element.
Eg:
1.<!DOCTYPEhtml>
<html>
<head>
<title>eg</title>
<scripttype=”text/javascript”src=”example1.js”></script>
</head>
<body>contentin there</body>
</html>
2.formore speed to loading javascript
thereuse javascript in <body></body>
3.deferand ajax
<scripttype=”text/javascript” defer=”defer” scr=example1.js></script>
<scripttype=”text/javascript” defer=”defer” scr=example2.js></script>
whenopen explorer 。htmlrequire script First come, first served。souse example1 for one
example2for two。butin reality life defer not obey this rules;sobetter use only one defer script
anduse defer in html down。
4.ajaxscript
asyncis different from defer. Because the async no first come,firstserverd.
5.inXHTML ways
extensiblehypertext markup language (可扩展的超文本标记语言)
rulesis so serious
usecdata conclude xml code
eg:
<scripttype=”text/javascript”><!【CDATA【
functioncompare(a,b)
if(a<B){…..................... }
]]></script>
ifexplorer doesn't support for xml
canuse javascript notes like //<!CDATA[
//]]>
Embedded(嵌入)code andexternal file
Advantagefor external file
maintainability(可维护性)
cache(缓存)oncefor two html
future
filemode
Doctype
1.quirksmode(混杂模式)
2.standardsmode(标准模式)
ifexplorer don‘t support for javascript
we’dbetter
<noscript>
<p>htmlrequire explorer support javascript
</noscript>
summary
Useexternal file for javascript need<script>src property for URL
thenis some rules for javascript/