使用 CSS 返回元数据
我有一个在服务器上动态创建的 CSS 样式表,并通过 标记返回。是否可以使用此样式表返回我可以使用 JavaScript 读取的任何元数据?
(用例:我返回的样式表是几个较小的样式表的组合。我希望我的 JavaScript 代码能够检测包含哪些较小的样式表。)我
考虑向元素添加一些自定义属性:
body {
-my-custom-prop1:0;
-my-custom-prop2:0;
}
但是当我尝试阅读时这些:
window.getComputedStyle(document.body)['-my-custom-prop1']
它们不会被退回。还有其他想法吗?
编辑:我最终采取了略有不同的方法。我没有添加 标记,而是发出 AJAX 请求来获取样式表,并将其文本添加到
标记中。这样我就可以使用 HTTP 响应标头来包含元数据。当然,这不能像
标签那样跨域工作。
I have a CSS stylesheet that's dynamically created on the server, and returned via a <link>
tag. Is it possible to return any metadata with this stylesheet, that I could read with JavaScript?
(The use case: the stylesheet I return is a combination of several smaller ones. I want my JavaScript code to be able to detect which smaller ones were included.)
I considered adding some custom properties to an element:
body {
-my-custom-prop1:0;
-my-custom-prop2:0;
}
But when I try to read these with:
window.getComputedStyle(document.body)['-my-custom-prop1']
they're not returned. Any other ideaas?
EDIT: I ended up taking a slightly different approach. Instead of adding a <link>
tag, I made an AJAX request to get the stylesheet, and added its text to a <style>
tag. This way I could use the HTTP response headers to include metadata. Of course, this won't work across domains, like a <link>
tag does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看以下示例→
虽然我认为这种技术是不明智的,这是我开发的一些东西,我已经测试过它可以在 Chrome、FF、Safari 和 IE8 中运行。
首先,我选择了
list-style-image
属性来存储元数据,因为它可以包含url()
参数中的任何字符串,但实际上并没有在任何正常情况下都会在 body CSS 中使用。然后,我为
getCompulatedStyle
实现了一个通用的跨浏览器函数,因为该函数并非在所有浏览器中都可用。然后我解析了 return 属性以仅获取
url('')
中的数据,从而产生以下函数:如果您需要存储多条信息,您可以用逗号分隔数据或甚至可能存储 JSON 字符串。我希望您有充分的理由想要这样做,因为我认为有更好的方法来存储元数据......但是就这样!
查看示例→
See example of the following →
Though I think this technique is ill-advised, here's something I developed that I've tested to work in Chrome, FF, Safari and IE8.
First, I picked the
list-style-image
property to be used to store the meta data since it can contain any string in theurl()
parameter and yet wasn't going to be used under any normal circumstances in the body CSS.Then I implemented a common cross-browser function to
getComputedStyle
since this isn't available in all browsers.Then I parsed the return property to get only the data within the
url('')
, resulting in these functions:If you need to store more than one piece of information you could comma-delineate the data or potentially even store a JSON string. I hope you have a good reason for wanting to do this as I think there are better ways to store meta data... but there you go!
See example →
返回的对象实际上代表 CSS 2.1 使用的值,而不是计算的值。
不过,您也许可以通过另一种方法询问这些样式:
http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
The returned object actually represents the CSS 2.1 used values, not the computed values. https://developer.mozilla.org/en/DOM/window.getComputedStyle
You might be able to interrogate these styles via another method though:
http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
不久前我问了一个相关问题。事实证明,您必须使用 JavaScript 手动解析样式表文本。我认为这不值得麻烦,并找到了解决我的问题的不同方法。我猜你可以使用一些聪明的技巧,比如虚假类的标准属性会起作用。
I asked a related question a while ago. It turns out that you have to parse the stylesheet text manually with javascript. I decided it was not worth the bother and found a different solution to my problem. You could use some clever tricks like standard properties on bogus classes would work I guess.