在If条件中使用window.location在CSS文件之间切换

Use window.location in If condition to toggle between CSS files

本文关键字:CSS 文件 之间 location window If 条件      更新时间:2023-09-26

我正在使用以下代码加载一个基于window.location元素的css文件。

<core:if test="${window.location.hostname == 'localhost'}">
<link href="././css/imageload.css" media="screen" rel="stylesheet" type="text/css" />
</core:if>

尽管window.location.hostname为"localhost",但测试始终返回false事件。这是我用一个警告框测试过的。

我是不是做错了什么?或者有其他方法可以比较window.location.hostname元素吗?

(我使用的是Spring框架。前端是Javascript+CSS)

不知道弹簧。核心:if是一个标签,它似乎是spring的一部分,条件是js。可以一起使用吗?否则,您只能使用js来插入链接标记。则在网络浏览器中显示页面期间将调用该逻辑。

<script>
  if( window.location.hostname == 'localhost' ) {
    document.write( "<link href='...' />" );
  }
</script>

Core:if不是javascript关键字,在服务器端执行。

你需要一些在客户端执行的东西。您还需要动态加载css文件。您可以查看动态加载库,例如require.js。

更快的方法:

<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<core:set var="hostName"><%=request.getServerName()%></core:set>
<core:if test='${hostName == "localhost"}'>
    <link href="././css/imageload.css" media="screen" rel="stylesheet" type="text/css" />
</core:if>