global.js配置全局变量在其他html页面引用时不生效
在配置页面时引用了global中定义的全局变量CONTEXT_PATH,用于拼接生成访问路径,但不生效,CONTEXT_PATH访问不到,配置如下:
<script th:src="@{/js/global.js}"></script>
<script>
function refresh_kaptcha() {
const path = CONTEXT_PATH+"/kaptcha?p=" + Math.random();
$("#kaptcha").attr("src", path);
}
</script>
global.js配置
var CONTEXT_PATH = "/community";
查询资料后解决如下:
1.要使用window.CONTEXT_PATH访问
2.要把js文件当作module导入 type=“module”
3.global.js配置window.CONTEXT_PATH=“/community”;
<script th:src="@{/js/global.js}" type="module"></script>
<script>
function refresh_kaptcha() {
const path = window.CONTEXT_PATH+"/kaptcha?p=" + Math.random();
$("#kaptcha").attr("src", path);
}
</script>
global.js配置
window.CONTEXT_PATH="/community";