how2j.cn


工具版本兼容问题
javascript代码必须放在script标签中

script标签可以放在html的任何地方,一般建议放在head标签里

示例 1 : script标签   
示例 2 : javascript顺序执行   
示例 3 : 使用外部js文件   

示例 1 :

script标签

javascript都是放在script标签中的,一旦加载,就会执行
"); window.frames["iframe_show1067"].document.write(decodeHtml($("textarea#stepcodeTextarea1067").val())); window.frames["iframe_show1067"].document.close(); $(window.frames["iframe_show1067"]).load(function(){ $("#iframe_show1067").height($("#iframe_show1067").contents().find("body").height()+showittryitheight); }); $("#iframe_show1067").height($("#iframe_show1067").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <head> <script> document.write("这是 javascript"); </script> </head> </html>
<html>
  <head>
   <script>
      document.write("这是 javascript");
   </script>
  </head>
</html>
"); window.frames["iframe1067"].document.write(decodeHtml(code1067)); window.frames["iframe1067"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1067"]).load(function(){ $("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight); }); $("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight); alreadyWriteCode1067 = code1067; $("#rendering1067").hide(); $("#rendered1067").show(); } var tRereshRetry2DemoPanel1067 = setInterval(rereshRetry2DemoPanel1067,1000); var binded1067 = false; $("textarea#stepcodeTextarea1067").keyup(function(){ if(!binded1067){ $(window).bind('beforeunload',function(){ binded1067 = true; return "xxxx"; }); } var newCode = $(this).val() code1067 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1067!=newCode){ // window.frames["iframe1067"].document.write("
"); // window.frames["iframe1067"].document.write(decodeHtml($("textarea#stepcodeTextarea1067").val())); // window.frames["iframe1067"].document.close(); // $(window.frames["iframe1067"]).load(function(){ // $("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight); // }); // code1067 = newCode; // } }); $(".tryButton1067").click(function(){ $("#tryDiv1067").show(); $("#stepcodeTextarea1067").focus(); $("#stepcodeTextarea1067").height(200); $("#iframe1067").height(0); window.frames["iframe1067"].document.write("
"); window.frames["iframe1067"].document.write(decodeHtml($("textarea#stepcodeTextarea1067").val())); window.frames["iframe1067"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1067"]).load(function(){ $("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight); }); $("#iframe1067").height($("#iframe1067").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1067.focus(); editor1067.setSize(null, "250"); $("#rendering1067").hide(); $("#rendered1067").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1067 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1067"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1067.on("change",function(doc){ if(!binded1067){ $(window).bind('beforeunload',function(){ binded1067 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1067 = newCode; $("textarea#stepcodeTextarea1067").val(newCode); if(alreadyWriteCode1067!=code1067){ lastModifedTime1067 = new Date().getTime(); $("#rendering1067").show(); $("#rendered1067").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1067 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1067.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1067").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

javascript顺序执行

如果有多段script代码,会按照从上到下,顺序执行
"); window.frames["iframe_show1068"].document.write(decodeHtml($("textarea#stepcodeTextarea1068").val())); window.frames["iframe_show1068"].document.close(); $(window.frames["iframe_show1068"]).load(function(){ $("#iframe_show1068").height($("#iframe_show1068").contents().find("body").height()+showittryitheight); }); $("#iframe_show1068").height($("#iframe_show1068").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("第一段javascript"); </script> <script> document.write("第二段javascript"); </script>
   <script>
      document.write("第一段javascript");
   </script>

   <script>
      document.write("第二段javascript");
   </script>
"); window.frames["iframe1068"].document.write(decodeHtml(code1068)); window.frames["iframe1068"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1068"]).load(function(){ $("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight); }); $("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight); alreadyWriteCode1068 = code1068; $("#rendering1068").hide(); $("#rendered1068").show(); } var tRereshRetry2DemoPanel1068 = setInterval(rereshRetry2DemoPanel1068,1000); var binded1068 = false; $("textarea#stepcodeTextarea1068").keyup(function(){ if(!binded1068){ $(window).bind('beforeunload',function(){ binded1068 = true; return "xxxx"; }); } var newCode = $(this).val() code1068 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1068!=newCode){ // window.frames["iframe1068"].document.write("
"); // window.frames["iframe1068"].document.write(decodeHtml($("textarea#stepcodeTextarea1068").val())); // window.frames["iframe1068"].document.close(); // $(window.frames["iframe1068"]).load(function(){ // $("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight); // }); // code1068 = newCode; // } }); $(".tryButton1068").click(function(){ $("#tryDiv1068").show(); $("#stepcodeTextarea1068").focus(); $("#stepcodeTextarea1068").height(200); $("#iframe1068").height(0); window.frames["iframe1068"].document.write("
"); window.frames["iframe1068"].document.write(decodeHtml($("textarea#stepcodeTextarea1068").val())); window.frames["iframe1068"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1068"]).load(function(){ $("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight); }); $("#iframe1068").height($("#iframe1068").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1068.focus(); editor1068.setSize(null, "250"); $("#rendering1068").hide(); $("#rendered1068").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1068 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1068"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1068.on("change",function(doc){ if(!binded1068){ $(window).bind('beforeunload',function(){ binded1068 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1068 = newCode; $("textarea#stepcodeTextarea1068").val(newCode); if(alreadyWriteCode1068!=code1068){ lastModifedTime1068 = new Date().getTime(); $("#rendering1068").show(); $("#rendered1068").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1068 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1068.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1068").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

使用外部js文件

和css一样,当javascript代码特别多,并且都写在html里的时候,会显得比较繁杂,难以维护。
这个时候可以采用和css一样的手段,把javascript代码剥离出来,单独放在一个文件里,在html中引用该文件即可。
准备一个hello.js,里面的是内容是
document.write("hello javascript ");
注意,不要写 script标签
"); window.frames["iframe_show1069"].document.write(decodeHtml($("textarea#stepcodeTextarea1069").val())); window.frames["iframe_show1069"].document.close(); $(window.frames["iframe_show1069"]).load(function(){ $("#iframe_show1069").height($("#iframe_show1069").contents().find("body").height()+showittryitheight); }); $("#iframe_show1069").height($("#iframe_show1069").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <script src="http://127.0.0.1/study/hello.js"></script> </html>
<html>
  <script src="http://127.0.0.1/study/hello.js"></script>
</html>
"); window.frames["iframe1069"].document.write(decodeHtml(code1069)); window.frames["iframe1069"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1069"]).load(function(){ $("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight); }); $("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight); alreadyWriteCode1069 = code1069; $("#rendering1069").hide(); $("#rendered1069").show(); } var tRereshRetry2DemoPanel1069 = setInterval(rereshRetry2DemoPanel1069,1000); var binded1069 = false; $("textarea#stepcodeTextarea1069").keyup(function(){ if(!binded1069){ $(window).bind('beforeunload',function(){ binded1069 = true; return "xxxx"; }); } var newCode = $(this).val() code1069 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1069!=newCode){ // window.frames["iframe1069"].document.write("
"); // window.frames["iframe1069"].document.write(decodeHtml($("textarea#stepcodeTextarea1069").val())); // window.frames["iframe1069"].document.close(); // $(window.frames["iframe1069"]).load(function(){ // $("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight); // }); // code1069 = newCode; // } }); $(".tryButton1069").click(function(){ $("#tryDiv1069").show(); $("#stepcodeTextarea1069").focus(); $("#stepcodeTextarea1069").height(200); $("#iframe1069").height(0); window.frames["iframe1069"].document.write("
"); window.frames["iframe1069"].document.write(decodeHtml($("textarea#stepcodeTextarea1069").val())); window.frames["iframe1069"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1069"]).load(function(){ $("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight); }); $("#iframe1069").height($("#iframe1069").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1069.focus(); editor1069.setSize(null, "250"); $("#rendering1069").hide(); $("#rendered1069").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1069 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1069"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1069.on("change",function(doc){ if(!binded1069){ $(window).bind('beforeunload',function(){ binded1069 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1069 = newCode; $("textarea#stepcodeTextarea1069").val(newCode); if(alreadyWriteCode1069!=code1069){ lastModifedTime1069 = new Date().getTime(); $("#rendering1069").show(); $("#rendered1069").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1069 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1069.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1069").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-script标签 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图