2012-03-28 134 views
0

我正在使用下面的代码来加载基于window.location元素的css文件。在if条件中使用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)

+1

这段代码在JavaScript或Java中运行? – kirilloid 2012-03-28 07:12:21

+0

@kirilloid Javascript – Chillax 2012-03-28 07:14:13

+0

@Chillax不知道为什么你在@ -ing me:s – 2012-03-28 07:14:54

回答

0

不知道弹簧。核心:如果是标签,它似乎是春天的一部分,条件是js。是一起使用可能吗?否则你只能使用js来插入链接标签。那么在网页浏览器中显示页面时将调用该逻辑。

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

核心:如果不是javascript关键字并且执行服务器端。

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

0

一种更快捷的方式:

<%@ 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> 
相关问题