JavaScript基本语法

  1. 创建脚本块
  2. <scriptlanguage=”JavaScript”>
  3. JavaScriptcodegoeshere
  4. </script>
  5. 隐藏脚本代码
  6. <scriptlanguage=”JavaScript”>
  7. <!--
  8. document.write(“Hello”);
  9. //-->
  10. </script>
  11. 浏览器不支持的时候显示
  12. <noscript>
  13. Hellotothenon-JavaScriptbrowser.
  14. </noscript>
  15. 链接外部脚本文件
  16. <scriptlanguage=”JavaScript”src="/”youname.js"”></script>
  17. 注释脚本
  18. //Thisisacomment
  19. document.write(“Hello”);//Thisisacomment
  20. /*
  21. Allofthis
  22. isacomment
  23. */
  24. 输出到浏览器
  25. document.write(“<strong>输出内容</strong>”);
  26. 定义变量
  27. varmyVariable=“somevalue”;
  28. 字符串相加
  29. varmyString=“String1”+“String2”;
  30. 字符串搜索
  31. <scriptlanguage=”JavaScript”>
  32. <!--
  33. varmyVariable=“Hellothere”;
  34. vartherePlace=myVariable.search(“there”);
  35. document.write(therePlace);
  36. //-->
  37. </script>
  38. 字符串替换
  39. thisVar.replace(“Monday”,”Friday”);
  40. 格式化字串
  41. <scriptlanguage=”JavaScript”>
  42. <!--
  43. varmyVariable=“Hellothere”;
  44. document.write(myVariable.big()+“<br>”);
  45. document.write(myVariable.blink()+“<br>”);
  46. document.write(myVariable.bold()+“<br>”);
  47. document.write(myVariable.fixed()+“<br>”);
  48. document.write(myVariable.fontcolor(“red”)+“<br>”);
  49. document.write(myVariable.fontsize(“18pt”)+“<br>”);
  50. document.write(myVariable.italics()+“<br>”);
  51. document.write(myVariable.small()+“<br>”);
  52. document.write(myVariable.strike()+“<br>”);
  53. document.write(myVariable.sub()+“<br>”);
  54. document.write(myVariable.sup()+“<br>”);
  55. document.write(myVariable.toLowerCase()+“<br>”);
  56. document.write(myVariable.toUpperCase()+“<br>”);
  57. varfirstString=“MyString”;
  58. varfinalString=firstString.bold().toLowerCase().fontcolor(“red”);
  59. //-->
  60. </script>
  61. 创建数组
  62. <scriptlanguage=”JavaScript”>
  63. <!--
  64. varmyArray=newArray(5);
  65. myArray[0]=“FirstEntry”;
  66. myArray[1]=“SecondEntry”;
  67. myArray[2]=“ThirdEntry”;
  68. myArray[3]=“FourthEntry”;
  69. myArray[4]=“FifthEntry”;
  70. varanotherArray=newArray(“FirstEntry”,”SecondEntry”,”ThirdEntry”,”FourthEntry”,”FifthEntry”);
  71. //-->
  72. </script>
  73. 数组排序
  74. <scriptlanguage=”JavaScript”>
  75. <!--
  76. varmyArray=newArray(5);
  77. myArray[0]=“z”;
  78. myArray[1]=“c”;
  79. myArray[2]=“d”;
  80. myArray[3]=“a”;
  81. myArray[4]=“q”;
  82. document.write(myArray.sort());
  83. //-->
  84. </script>
  85. 分割字符串
  86. <scriptlanguage=”JavaScript”>
  87. <!--
  88. varmyVariable=“a,b,c,d”;
  89. varstringArray=myVariable.split(“,”);
  90. document.write(stringArray[0]);
  91. document.write(stringArray[1]);
  92. document.write(stringArray[2]);
  93. document.write(stringArray[3]);
  94. //-->
  95. </script>
  96. 弹出警告信息
  97. <scriptlanguage=”JavaScript”>
  98. <!--
  99. window.alert(“Hello”);
  100. //-->
  101. </script>
  102. 弹出确认框
  103. <scriptlanguage=”JavaScript”>
  104. <!--
  105. varresult=window.confirm(“ClickOKtocontinue”);
  106. //-->
  107. </script>
  108. 定义函数
  109. <scriptlanguage=”JavaScript”>
  110. <!--
  111. functionmultiple(number1,number2){
  112. varresult=number1*number2;
  113. returnresult;
  114. }
  115. //-->
  116. </script>
  117. 调用JS函数
  118. <ahref=”#”onClick=”functionName()”>Linktext</a>
  119. <ahref="/”javascript:functionName"()”>Linktext</a>
  120. 在页面加载完成后执行函数
  121. <bodyonLoad=”functionName();”>
  122. Bodyofthepage
  123. </body>
  124. 条件判断
  125. <script>
  126. <!--
  127. varuserChoice=window.confirm(“ChooseOKorCancel”);
  128. varresult=(userChoice==true)?“OK”:“Cancel”;
  129. document.write(result);
  130. //-->
  131. </script>
  132. 指定次数循环
  133. <script>
  134. <!--
  135. varmyArray=newArray(3);
  136. myArray[0]=“Item0”;
  137. myArray[1]=“Item1”;
  138. myArray[2]=“Item2”;
  139. for(i=0;i<myArray.length;i++){
  140. document.write(myArray+“<br>”);
  141. }
  142. //-->
  143. </script>
  144. 设定将来执行
  145. <script>
  146. <!--
  147. functionhello(){
  148. window.alert(“Hello”);
  149. }
  150. window.setTimeout(“hello()”,5000);
  151. //-->
  152. </script>
  153. 定时执行函数
  154. <script>
  155. <!--
  156. functionhello(){
  157. window.alert(“Hello”);
  158. window.setTimeout(“hello()”,5000);
  159. }
  160. window.setTimeout(“hello()”,5000);
  161. //-->
  162. </script>
  163. 取消定时执行
  164. <script>
  165. <!--
  166. functionhello(){
  167. window.alert(“Hello”);
  168. }
  169. varmyTimeout=window.setTimeout(“hello()”,5000);
  170. window.clearTimeout(myTimeout);
  171. //-->
  172. </script>
  173. 在页面卸载时候执行函数
  174. <bodyonUnload=”functionName();”>
  175. Bodyofthepage
  176. </body>

 

你可能感兴趣的:(JavaScript,C++,c,浏览器,脚本)