1

在 Gmail 中的 Chrome 或 Safari 检查器控制台中输入以下内容:

function load(url,cb){var x=document.body.appendChild(document.createElement('script'));x.src=url;x.onload=function(){console.log("Dynamically loaded "+url);if(cb){cb();}};if(!cb){x.setAttribute('async','')}}
load("https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js",function(){alert("Modernizr loaded");});

导致大部分文本的布局变得疯狂。

这可以解释吗?我希望我的工具(包括运行一个动态加载 Modernizr 的小书签)在 Gmail 上工作。

4

1 回答 1

2

Modernizer adds a lot of classes to the <html> tag, and one of those classes is js. I couldn't find anything in the documentation that describes what that represents, although I suspect it just represents JavaScript support. Google has minified their classes, so they are all short two letter items (probably generated). One of them happens to be js as well, which is causing a lot of text to get centered.

In fact, to un-hose it, just remove the js class from the <html> tag after dynamically loading your script and it fixes itself.

Here's a fixed version:

function load(url,cb){var x=document.body.appendChild(document.createElement('script'));x.src=url;x.onload=function(){console.log("Dynamically loaded "+url);if(cb){cb();}};if(!cb){x.setAttribute('async','')}}
load("https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js",function(){var htmlElement = document.getElementsByTagName("html")[0]; htmlElement.className = htmlElement.className.replace
      ( /(?:^|\s)js(?!\S)/g , '' );    alert("Modernizr loaded");});
于 2013-03-06T05:32:01.030 回答