以编程方式将 CSS 添加到 IE 时遇到问题

发布于 2024-07-15 23:20:28 字数 1115 浏览 3 评论 0原文

我有一个书签,它通过“链接”标签(外部样式表)将 CSS 样式表插入到目标 DOM 中。

最近,此功能在 Amazon.com 上停止运行,仅在 Internet Explorer 中运行。 它适用于其他网站和其他浏览器(甚至在 Amazon.com 上)。 我们用来插入样式表的技术非常简单:

document.getElementsByTagName('head')[0].appendChild(s);

其中“s”是使用 document.createElement 创建的链接对象。 即使在 Amazon 上,我也可以通过 Internet Explorer 开发人员工具栏 DOM 检查器看到该元素就在那里。 但是如果我在 JavaScript 中警告 document.styleSheets 集合,它就不在那里。

作为测试,我尝试使用仅 IE 文档.createStyleSheet 方法将 URL 作为参数传递给我的样式表。 这会引发错误:

没有足够的可用存储空间 完成此操作

兴趣点:

  • document.createStyleSheet 的文档表示,如果页面上有超过 31 个样式表,则会抛出错误,但 (1) 这是一个不同的错误,并且 (2) 那里页面上只有 10 个外部样式表。
  • 我在谷歌上搜索该错误发现了许多死胡同,唯一建议与样式表相关的内容是 this drupal post,但它指的是内联样式的字符限制,而不是与外部样式相关的问题。
  • 相同的代码,甚至是 createStyleSheet 调用,也适用于 IE 中的其他网站。

这对我来说已经达到了“完全神秘”的状态。

I have a bookmarklet which inserts a CSS stylesheet into the target DOM via a "link" tag (external stylesheet).

Recently, this stopped working on Amazon.com, in Internet Explorer only. It works on other sites, and with other browsers (even on Amazon.com). The technique we're using to insert the stylesheet is pretty straightforward:

document.getElementsByTagName('head')[0].appendChild(s);

Where "s" is a link object created with document.createElement. Even on Amazon, I see via the Internet Explorer Developer Toolbar DOM inspector that the element is there. However if I alert the document.styleSheets collection in JavaScript, it's not there.

As a test, I tried to use the IE-only document.createStyleSheet method passing the URL to my stylesheet as an argument. This throws the error:

Not enough storage is available to
complete this operation

Points of interest:

  • The documentation for document.createStyleSheet says an error will be thrown if there are more than 31 stylesheets on the page but (1) it's a different error, and (2) there are only 10 external stylesheets on the page.
  • My googling for the error turned up a number of dead-ends, and the only one that suggested anything stylesheet-related was this drupal post, but it refers to a character limit on inline styles, as opposed to a problem relating to external styles.
  • The same code, even the createStyleSheet call, works on other sites in IE.

This has reached "complete mystery" status for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

下壹個目標 2024-07-22 23:20:28

我刚刚

javascript:(function(d) { d.createStyleSheet().cssText="* {color:blue !important;}"   })(document);

javascript:(function(d) { d.createStyleSheet("http://myschemas.com/pub/clear.css") })(document);

在 amazon.com 上从 IE 尝试过这个,并且都有效
也许您需要将 !important 添加到 css 的某些项目中以确保它们立即生效?

更新:
为您找到了可能的解决方案...

javascript:(function(c) {c[c.length-1].addImport("http://myschemas.com/pub/clear.css")})(document.styleSheets);

希望它对您有帮助。

I just tried this

javascript:(function(d) { d.createStyleSheet().cssText="* {color:blue !important;}"   })(document);

and

javascript:(function(d) { d.createStyleSheet("http://myschemas.com/pub/clear.css") })(document);

from IE on amazon.com and both worked.
Maybe you need to add the !important to some items of your css to be sure they take effect now?

UPDATE:
Found a possible solution for you...

javascript:(function(c) {c[c.length-1].addImport("http://myschemas.com/pub/clear.css")})(document.styleSheets);

Hope it helps you.

糖粟与秋泊 2024-07-22 23:20:28

在寻找答案时,我发现以编程方式加载 CSS 时,31 个样式表限制会引发此异常:

http://www.telerik.com/community/forums/aspnet-ajax/general-discussions/not-enough- storage-is-available-to-complete-this-operation.aspx

知识库文档中描述了原始限制(假设仅发生在 IE8 及更低版本上,但多次报告发生在 IE9 中):

http://support.microsoft.com/kb/262161

Looking for an answer, I have found that the 31 stylesheets limit raise this exception when loading CSS programatically:

http://www.telerik.com/community/forums/aspnet-ajax/general-discussions/not-enough-storage-is-available-to-complete-this-operation.aspx

The original limitation is described in a Knowledge Base document (suppossed only to happen on IE8 and below, but repeatedly reported as happening in IE9):

http://support.microsoft.com/kb/262161

み格子的夏天 2024-07-22 23:20:28

这就是我所做的一切,我从未见过它不起作用。

loadCss = function( name, url ) {

    var head = document.getElementsByTagName('head')[0];

    var link = document.createElement("link");

    link.setAttribute("type", "text/css");

    link.setAttribute("rel", "stylesheet");

    link.setAttribute("href", url);

    link.setAttribute("media", "screen");

    head.appendChild(link);
};

This is all I do, and it I have never seen it not work.

loadCss = function( name, url ) {

    var head = document.getElementsByTagName('head')[0];

    var link = document.createElement("link");

    link.setAttribute("type", "text/css");

    link.setAttribute("rel", "stylesheet");

    link.setAttribute("href", url);

    link.setAttribute("media", "screen");

    head.appendChild(link);
};
冷心人i 2024-07-22 23:20:28

我正在使用 jQuery 做类似的事情,发现我必须按以下顺序执行:

var $link = $('<link>');
$('head').add($link);
$link.attr({
  type: 'text/css',
  // ... etc ...
  media: 'screen'
});

否则它在 IE 中不起作用(IE7,还没有看过 IE8)。

华泰

I was doing something similar using jQuery and found that I had to do it in this order:

var $link = $('<link>');
$('head').add($link);
$link.attr({
  type: 'text/css',
  // ... etc ...
  media: 'screen'
});

Otherwise it doesn't work in IE (IE7, haven't looked at IE8 yet).

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文