我可以根据要求加载外部样式表吗?
$.getScript('ajax/test.js', function() {
alert('Load was performed.');
});
.. 就像上面根据请求加载外部 JS 的代码一样,是否有类似的东西可以在需要时加载外部 CSS 样式表?
例如,当我在网站上使用灯箱(内联弹出窗口)时,我希望避免加载灯箱 JS 和 CSS 文件,除非用户请求。
谢谢
$.getScript('ajax/test.js', function() {
alert('Load was performed.');
});
.. like the above code which loads an external JS on request, is there something similar available to load an external CSS stylesheet when required?
Like for example when I use lightboxes (inline popups) on my site, I want to avoid loading lightbox JS and CSS files onload, unless requested by the user.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以这样做:
You could do:
您只需在 head 中添加动态 HTML 内容即可添加对外部样式表的引用:
此内容将插入到 DOM 中,然后正常呈现,在这种情况下会导致获取外部样式表。
不过,也要注意 cletus 的回答,如果您不小心动态加载静态内容,最终可能会花费您的页面加载时间和过多的资源传输。
You can add references to external stylesheets simply by adding dynamic HTML content in the head:
This content gets inserted in the DOM, and subsequently rendered normally, in this case causing a fetch of an external stylesheet.
Pay attention to cletus' answer as well however, as if you're not careful with dynamic loading of static content, it can end up costing you in page load time and excessive resource transfer.
一般来说,如果你做得正确的话,最好只包含你需要的所有内容。
我的意思是,静态内容(图像、Javascript、CSS)的最佳实践是:
因此用户只会下载给定文件一次,直到它发生更改。
动态加载 CSS 和 Javascript,除非特别大,否则通常是没有根据的并且会适得其反。
Generally you're better off just including all you need assuming you're doing so correctly.
By that I mean that best practice for static content (images, Javascript, CSS) is to:
so a user will only ever download a given file once until it changes.
Dynamically loading CSS and Javascript, unless it is exceptionally large, is typically unwarranted and counterproductive.
IE 问题...
我使 IE 6、7、8 崩溃,只需
将它们复制到主表中以避免另一个 http 请求,瞧。
IE issues...
I was crashing IE 6,7,8 with
Just copied them into the main sheet to avoid another http req, voila.
我们可以直接附加它
We can append it directly by
是的:如果您创建链接到样式表的
标记并将其添加到
标记,浏览器将加载该样式表。
例如
但是,根据@peteorpeter的评论,这在 IE 8 或更低版本中不起作用 - 在那里,您需要:
href
>;或者document.createStyleSheet()
方法此外,检查链接是否已添加并不能在所有浏览器中可靠地工作。
我也会质疑你的部分前提:
为什么?减少页面重量?我可以理解这种愿望,但你应该测量 CSS 和 JS 文件的大小(缩小和压缩后),其中是否包含灯箱代码,看看减少是否值得:
在缩小和 gzip 压缩之后,很可能不会有太大差异。
请记住,您可以指示浏览器长时间缓存您的 CSS 和 JS,这意味着只有当用户访问您的缓存为空的网站时才会下载它。
Yup: if you create a
<link>
tag linking to a stylesheet and add it to the<head>
tag, the browser will load that stylesheet.E.g.
However, as per @peteorpeter’s comments, this doesn’t work in IE 8 or under — there, you need to either:
<link>
before setting itshref
; ordocument.createStyleSheet()
methodAlso, checking whether a link has already been added doesn’t work reliably across all browsers.
I would also question part of your premise:
Why? To reduce page weight? I can understand the desire, but you should measure the size of your CSS and JS files (after minification and gzipping) with the lightbox code in there, and without, to see whether the reduction is worth:
After minification and gzipping, there may well not be that much difference.
And bear in mind that you can instruct the browser to cache your CSS and JS for a long time, meaning it only gets downloaded when a user visits your site with an empty cache.