window.location.href 放置在单独的JS文件中使用时问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了window.location.href 放置在单独的JS文件中使用时问题相关的知识,希望对你有一定的参考价值。
场景:假设当前浏览器地址栏的地址是:http://localhost:8888/SSHBoot/tourist/homeMainAction_signInUI.do,
现在我想在点击按钮时定位到“http://localhost:8888/SSHBoot/member/adminMainAction_mainUI.do”这个地址
以下js代码是没问题,将js脚本放置到页面中
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>BootStrap学习</title> </head> <body> <input id="btnSubmit" class="btn btn-success btn-block btn-lg" type="button" value="点击"> <script type="text/javascript" src="${basePath}/js/homeMainAction/signInUI.js" CHARSET="utf-8"></script>
<script type="text/javascript">
$(function($) {
// 登录点击
$("#btnSubmit").click(function() {
window.location.href = "http://localhost:8888/SSHBoot/member/adminMainAction_mainUI.do";
});
});
</script>
</body> </html>
但是,如果我把上面的js脚本提取出来放置到“signInUI.js”文件中,如下。这样方式就不能达到预想的效果:
浏览器地址栏中的地址就是: http://localhost:8888/SSHBoot/tourist/http://localhost:8888/member/index.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>BootStrap学习</title> </head> <body> <input id="btnSubmit" class="btn btn-success btn-block btn-lg" type="button" value="点击"> <script type="text/javascript" src="${basePath}/js/homeMainAction/signInUI.js" CHARSET="utf-8"></script> </body> </html>
signInUI.js:
$(function($) { // 登录点击 $("#btnSubmit").click(function() { window.location.href = "http://localhost:8888/SSHBoot/member/adminMainAction_mainUI.do"; }); });
这个时候应该采用以下的路径样式:
$(function($) { // 登录点击 $("#btnSubmit").click(function() { window.location.href = "../member/adminMainAction_mainUI.do"; }); });
以上是关于window.location.href 放置在单独的JS文件中使用时问题的主要内容,如果未能解决你的问题,请参考以下文章
window.location和window.location.href的区别
一个关于window.location.href=window.location.href的问题?