how2j.cn


工具版本兼容问题
Location表示浏览器中的地址栏

示例 1 : 刷新当前页面   
示例 2 : 跳转到另一个页面   
示例 3 : Location的其他属性   

示例 1 :

刷新当前页面

reload方法刷新当前页面
"); window.frames["iframe_show1163"].document.write(decodeHtml($("textarea#stepcodeTextarea1163").val())); window.frames["iframe_show1163"].document.close(); $(window.frames["iframe_show1163"]).load(function(){ $("#iframe_show1163").height($("#iframe_show1163").contents().find("body").height()+showittryitheight); }); $("#iframe_show1163").height($("#iframe_show1163").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<span>当前时间:</span> <script> var d = new Date(); document.write(d.getHours()); document.write(":"); document.write(d.getMinutes()); document.write(":"); document.write(d.getSeconds()); document.write(":"); document.write(d.getMilliseconds()); function refresh(){ location.reload(); } </script> <br> <button onclick="refresh()">刷新当前页面</button>
<span>当前时间:</span>
<script>
  var d = new Date();
  document.write(d.getHours());
  document.write(":");
  document.write(d.getMinutes());
  document.write(":");
  document.write(d.getSeconds());
  document.write(":");
  document.write(d.getMilliseconds());

function refresh(){
  location.reload();
}
</script>

<br>
<button onclick="refresh()">刷新当前页面</button>
"); window.frames["iframe1163"].document.write(decodeHtml(code1163)); window.frames["iframe1163"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1163"]).load(function(){ $("#iframe1163").height($("#iframe1163").contents().find("body").height()+showittryitheight); }); $("#iframe1163").height($("#iframe1163").contents().find("body").height()+showittryitheight); alreadyWriteCode1163 = code1163; $("#rendering1163").hide(); $("#rendered1163").show(); } var tRereshRetry2DemoPanel1163 = setInterval(rereshRetry2DemoPanel1163,1000); var binded1163 = false; $("textarea#stepcodeTextarea1163").keyup(function(){ if(!binded1163){ $(window).bind('beforeunload',function(){ binded1163 = true; return "xxxx"; }); } var newCode = $(this).val() code1163 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1163!=newCode){ // window.frames["iframe1163"].document.write("
"); // window.frames["iframe1163"].document.write(decodeHtml($("textarea#stepcodeTextarea1163").val())); // window.frames["iframe1163"].document.close(); // $(window.frames["iframe1163"]).load(function(){ // $("#iframe1163").height($("#iframe1163").contents().find("body").height()+showittryitheight); // }); // code1163 = newCode; // } }); $(".tryButton1163").click(function(){ $("#tryDiv1163").show(); $("#stepcodeTextarea1163").focus(); $("#stepcodeTextarea1163").height(200); $("#iframe1163").height(0); window.frames["iframe1163"].document.write("
"); window.frames["iframe1163"].document.write(decodeHtml($("textarea#stepcodeTextarea1163").val())); window.frames["iframe1163"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1163"]).load(function(){ $("#iframe1163").height($("#iframe1163").contents().find("body").height()+showittryitheight); }); $("#iframe1163").height($("#iframe1163").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1163.focus(); editor1163.setSize(null, "250"); $("#rendering1163").hide(); $("#rendered1163").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 editor1163 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1163"), { 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); } } }); editor1163.on("change",function(doc){ if(!binded1163){ $(window).bind('beforeunload',function(){ binded1163 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1163 = newCode; $("textarea#stepcodeTextarea1163").val(newCode); if(alreadyWriteCode1163!=code1163){ lastModifedTime1163 = new Date().getTime(); $("#rendering1163").show(); $("#rendered1163").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1163 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1163.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1163").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

跳转到另一个页面

"); window.frames["iframe_show1164"].document.write(decodeHtml($("textarea#stepcodeTextarea1164").val())); window.frames["iframe_show1164"].document.close(); $(window.frames["iframe_show1164"]).load(function(){ $("#iframe_show1164").height($("#iframe_show1164").contents().find("body").height()+showittryitheight); }); $("#iframe_show1164").height($("#iframe_show1164").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function jump(){ //方法1 //location="/"; //方法2 location.assign("/"); } </script> <br> <button onclick="jump()">跳转到首页</button>
<script>
function jump(){
  //方法1
  //location="/";

  //方法2
  location.assign("/");
  
}
</script>

<br>
<button onclick="jump()">跳转到首页</button>
"); window.frames["iframe1164"].document.write(decodeHtml(code1164)); window.frames["iframe1164"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1164"]).load(function(){ $("#iframe1164").height($("#iframe1164").contents().find("body").height()+showittryitheight); }); $("#iframe1164").height($("#iframe1164").contents().find("body").height()+showittryitheight); alreadyWriteCode1164 = code1164; $("#rendering1164").hide(); $("#rendered1164").show(); } var tRereshRetry2DemoPanel1164 = setInterval(rereshRetry2DemoPanel1164,1000); var binded1164 = false; $("textarea#stepcodeTextarea1164").keyup(function(){ if(!binded1164){ $(window).bind('beforeunload',function(){ binded1164 = true; return "xxxx"; }); } var newCode = $(this).val() code1164 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1164!=newCode){ // window.frames["iframe1164"].document.write("
"); // window.frames["iframe1164"].document.write(decodeHtml($("textarea#stepcodeTextarea1164").val())); // window.frames["iframe1164"].document.close(); // $(window.frames["iframe1164"]).load(function(){ // $("#iframe1164").height($("#iframe1164").contents().find("body").height()+showittryitheight); // }); // code1164 = newCode; // } }); $(".tryButton1164").click(function(){ $("#tryDiv1164").show(); $("#stepcodeTextarea1164").focus(); $("#stepcodeTextarea1164").height(200); $("#iframe1164").height(0); window.frames["iframe1164"].document.write("
"); window.frames["iframe1164"].document.write(decodeHtml($("textarea#stepcodeTextarea1164").val())); window.frames["iframe1164"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1164"]).load(function(){ $("#iframe1164").height($("#iframe1164").contents().find("body").height()+showittryitheight); }); $("#iframe1164").height($("#iframe1164").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1164.focus(); editor1164.setSize(null, "250"); $("#rendering1164").hide(); $("#rendered1164").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 editor1164 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1164"), { 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); } } }); editor1164.on("change",function(doc){ if(!binded1164){ $(window).bind('beforeunload',function(){ binded1164 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1164 = newCode; $("textarea#stepcodeTextarea1164").val(newCode); if(alreadyWriteCode1164!=code1164){ lastModifedTime1164 = new Date().getTime(); $("#rendering1164").show(); $("#rendered1164").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1164 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1164.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1164").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

Location的其他属性

"); window.frames["iframe_show1165"].document.write(decodeHtml($("textarea#stepcodeTextarea1165").val())); window.frames["iframe_show1165"].document.close(); $(window.frames["iframe_show1165"]).load(function(){ $("#iframe_show1165").height($("#iframe_show1165").contents().find("body").height()+showittryitheight); }); $("#iframe_show1165").height($("#iframe_show1165").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function p(s){ document.write(s); document.write("<br>"); } p("协议 location.protocol:"+location.protocol); p("主机名 location.hostname:"+location.hostname); p("端口号 (默认是80,没有即表示80端口)location.port:"+location.port); p("主机加端口号 location.host:"+location.host); p("访问的路径 location.pathname:"+location.pathname); p("锚点 location.hash:"+location.hash); p("参数列表 location.search"+location.search); </script>
<script>
function p(s){
  document.write(s);
  document.write("<br>");
}

p("协议 location.protocol:"+location.protocol);
p("主机名 location.hostname:"+location.hostname);
p("端口号 (默认是80,没有即表示80端口)location.port:"+location.port);

p("主机加端口号 location.host:"+location.host);
p("访问的路径  location.pathname:"+location.pathname);

p("锚点 location.hash:"+location.hash);
p("参数列表 location.search"+location.search);

</script>

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


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


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


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

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

上传截图