how2j.cn


工具版本兼容问题
BOM即 浏览器对象模型(Brower Object Model)

浏览器对象包括
Window(窗口)
Navigator(浏览器)
Screen (客户端屏幕)
History(访问历史)
Location(浏览器地址)

本章节从 Window(窗口)开始讲起

示例 1 : 获取文档显示区域的高度和宽度   
示例 2 : 获取外部窗体的宽度和高度   
示例 3 : 打开一个新的窗口   

示例 1 :

获取文档显示区域的高度和宽度

一旦页面加载,就会自动创建window对象,所以无需手动创建window对象。
通过window对象可以获取文档显示区域的高度和宽度
"); window.frames["iframe_show1156"].document.write(decodeHtml($("textarea#stepcodeTextarea1156").val())); window.frames["iframe_show1156"].document.close(); $(window.frames["iframe_show1156"]).load(function(){ $("#iframe_show1156").height($("#iframe_show1156").contents().find("body").height()+showittryitheight); }); $("#iframe_show1156").height($("#iframe_show1156").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("文档内容"); document.write("文档显示区域的宽度"+window.innerWidth); document.write("<br>"); document.write("文档显示区域的高度"+window.innerHeight); </script>
<script>
  document.write("文档内容");
  document.write("文档显示区域的宽度"+window.innerWidth);
  document.write("<br>");
  document.write("文档显示区域的高度"+window.innerHeight);
</script>
"); window.frames["iframe1156"].document.write(decodeHtml(code1156)); window.frames["iframe1156"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1156"]).load(function(){ $("#iframe1156").height($("#iframe1156").contents().find("body").height()+showittryitheight); }); $("#iframe1156").height($("#iframe1156").contents().find("body").height()+showittryitheight); alreadyWriteCode1156 = code1156; $("#rendering1156").hide(); $("#rendered1156").show(); } var tRereshRetry2DemoPanel1156 = setInterval(rereshRetry2DemoPanel1156,1000); var binded1156 = false; $("textarea#stepcodeTextarea1156").keyup(function(){ if(!binded1156){ $(window).bind('beforeunload',function(){ binded1156 = true; return "xxxx"; }); } var newCode = $(this).val() code1156 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1156!=newCode){ // window.frames["iframe1156"].document.write("
"); // window.frames["iframe1156"].document.write(decodeHtml($("textarea#stepcodeTextarea1156").val())); // window.frames["iframe1156"].document.close(); // $(window.frames["iframe1156"]).load(function(){ // $("#iframe1156").height($("#iframe1156").contents().find("body").height()+showittryitheight); // }); // code1156 = newCode; // } }); $(".tryButton1156").click(function(){ $("#tryDiv1156").show(); $("#stepcodeTextarea1156").focus(); $("#stepcodeTextarea1156").height(200); $("#iframe1156").height(0); window.frames["iframe1156"].document.write("
"); window.frames["iframe1156"].document.write(decodeHtml($("textarea#stepcodeTextarea1156").val())); window.frames["iframe1156"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1156"]).load(function(){ $("#iframe1156").height($("#iframe1156").contents().find("body").height()+showittryitheight); }); $("#iframe1156").height($("#iframe1156").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1156.focus(); editor1156.setSize(null, "250"); $("#rendering1156").hide(); $("#rendered1156").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 editor1156 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1156"), { 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); } } }); editor1156.on("change",function(doc){ if(!binded1156){ $(window).bind('beforeunload',function(){ binded1156 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1156 = newCode; $("textarea#stepcodeTextarea1156").val(newCode); if(alreadyWriteCode1156!=code1156){ lastModifedTime1156 = new Date().getTime(); $("#rendering1156").show(); $("#rendered1156").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1156 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1156.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1156").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

获取外部窗体的宽度和高度

所谓的外部窗体即浏览器,可能用的是360,火狐,IE, Chrome等等。
"); window.frames["iframe_show1157"].document.write(decodeHtml($("textarea#stepcodeTextarea1157").val())); window.frames["iframe_show1157"].document.close(); $(window.frames["iframe_show1157"]).load(function(){ $("#iframe_show1157").height($("#iframe_show1157").contents().find("body").height()+showittryitheight); }); $("#iframe_show1157").height($("#iframe_show1157").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> document.write("浏览器的宽度:"+window.outerWidth); document.write("<br>"); document.write("浏览器的高度:"+window.outerHeight); </script>
<script>

  document.write("浏览器的宽度:"+window.outerWidth);
  document.write("<br>");
  document.write("浏览器的高度:"+window.outerHeight);

</script>
"); window.frames["iframe1157"].document.write(decodeHtml(code1157)); window.frames["iframe1157"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1157"]).load(function(){ $("#iframe1157").height($("#iframe1157").contents().find("body").height()+showittryitheight); }); $("#iframe1157").height($("#iframe1157").contents().find("body").height()+showittryitheight); alreadyWriteCode1157 = code1157; $("#rendering1157").hide(); $("#rendered1157").show(); } var tRereshRetry2DemoPanel1157 = setInterval(rereshRetry2DemoPanel1157,1000); var binded1157 = false; $("textarea#stepcodeTextarea1157").keyup(function(){ if(!binded1157){ $(window).bind('beforeunload',function(){ binded1157 = true; return "xxxx"; }); } var newCode = $(this).val() code1157 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1157!=newCode){ // window.frames["iframe1157"].document.write("
"); // window.frames["iframe1157"].document.write(decodeHtml($("textarea#stepcodeTextarea1157").val())); // window.frames["iframe1157"].document.close(); // $(window.frames["iframe1157"]).load(function(){ // $("#iframe1157").height($("#iframe1157").contents().find("body").height()+showittryitheight); // }); // code1157 = newCode; // } }); $(".tryButton1157").click(function(){ $("#tryDiv1157").show(); $("#stepcodeTextarea1157").focus(); $("#stepcodeTextarea1157").height(200); $("#iframe1157").height(0); window.frames["iframe1157"].document.write("
"); window.frames["iframe1157"].document.write(decodeHtml($("textarea#stepcodeTextarea1157").val())); window.frames["iframe1157"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1157"]).load(function(){ $("#iframe1157").height($("#iframe1157").contents().find("body").height()+showittryitheight); }); $("#iframe1157").height($("#iframe1157").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1157.focus(); editor1157.setSize(null, "250"); $("#rendering1157").hide(); $("#rendered1157").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 editor1157 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1157"), { 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); } } }); editor1157.on("change",function(doc){ if(!binded1157){ $(window).bind('beforeunload',function(){ binded1157 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1157 = newCode; $("textarea#stepcodeTextarea1157").val(newCode); if(alreadyWriteCode1157!=code1157){ lastModifedTime1157 = new Date().getTime(); $("#rendering1157").show(); $("#rendered1157").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1157 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1157.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1157").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

打开一个新的窗口

有的时候,你碰到一些网站会自动打开另一个网站,那么是怎么做到的呢?
就是通过window的open方法做到的
不建议使用,如果需要打开一个新的网站,应该通过超级链接等方式让用户主动打开,在没有告知用户的前提下就打开一个新的网站会影响用户的体验
"); window.frames["iframe_show1158"].document.write(decodeHtml($("textarea#stepcodeTextarea1158").val())); window.frames["iframe_show1158"].document.close(); $(window.frames["iframe_show1158"]).load(function(){ $("#iframe_show1158").height($("#iframe_show1158").contents().find("body").height()+showittryitheight); }); $("#iframe_show1158").height($("#iframe_show1158").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function openNewWindow(){ myWindow=window.open("/"); } </script> <button onclick="openNewWindow()">打开一个新的窗口</button>
<script>
function openNewWindow(){
  myWindow=window.open("/");
}
</script>
 
<button onclick="openNewWindow()">打开一个新的窗口</button>

"); window.frames["iframe1158"].document.write(decodeHtml(code1158)); window.frames["iframe1158"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1158"]).load(function(){ $("#iframe1158").height($("#iframe1158").contents().find("body").height()+showittryitheight); }); $("#iframe1158").height($("#iframe1158").contents().find("body").height()+showittryitheight); alreadyWriteCode1158 = code1158; $("#rendering1158").hide(); $("#rendered1158").show(); } var tRereshRetry2DemoPanel1158 = setInterval(rereshRetry2DemoPanel1158,1000); var binded1158 = false; $("textarea#stepcodeTextarea1158").keyup(function(){ if(!binded1158){ $(window).bind('beforeunload',function(){ binded1158 = true; return "xxxx"; }); } var newCode = $(this).val() code1158 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1158!=newCode){ // window.frames["iframe1158"].document.write("
"); // window.frames["iframe1158"].document.write(decodeHtml($("textarea#stepcodeTextarea1158").val())); // window.frames["iframe1158"].document.close(); // $(window.frames["iframe1158"]).load(function(){ // $("#iframe1158").height($("#iframe1158").contents().find("body").height()+showittryitheight); // }); // code1158 = newCode; // } }); $(".tryButton1158").click(function(){ $("#tryDiv1158").show(); $("#stepcodeTextarea1158").focus(); $("#stepcodeTextarea1158").height(200); $("#iframe1158").height(0); window.frames["iframe1158"].document.write("
"); window.frames["iframe1158"].document.write(decodeHtml($("textarea#stepcodeTextarea1158").val())); window.frames["iframe1158"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1158"]).load(function(){ $("#iframe1158").height($("#iframe1158").contents().find("body").height()+showittryitheight); }); $("#iframe1158").height($("#iframe1158").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1158.focus(); editor1158.setSize(null, "250"); $("#rendering1158").hide(); $("#rendered1158").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 editor1158 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1158"), { 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); } } }); editor1158.on("change",function(doc){ if(!binded1158){ $(window).bind('beforeunload',function(){ binded1158 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1158 = newCode; $("textarea#stepcodeTextarea1158").val(newCode); if(alreadyWriteCode1158!=code1158){ lastModifedTime1158 = new Date().getTime(); $("#rendering1158").show(); $("#rendered1158").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1158 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1158.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1158").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


关于 前端部分-JavaScript-Window 的提问

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

上传截图