简体   繁体   English

加载DOM和CSS但不加载图像后,如何触发javascript?

[英]How do I fire javascript after DOM and CSS are loaded but not images?

I need to fire some javascript after the DOM and CSS is loaded, but not images. 在加载DOM和CSS之后,我需要启动一些JavaScript,但不能启动图片。 I believe that's after document.ready but before window.onload. 我相信那是在document.ready之后,但在window.onload之前。 Anyone know how to achieve this? 有人知道如何实现这一目标吗?

I was thinking of loading CSS after document.ready by iterating through document.styleSheets but it feels like a bad idea. 我本来是想通过document.styleSheets进行迭代在document.styleSheets之后加载CSS,但这感觉不是一个好主意。

Update: The reason for this unusual requirement is that I need to predict the size which images will be rendered, which is determined by css. 更新:出现这种异常要求的原因是,我需要预测将要渲染的图像的大小,该大小由css确定。 Moving the javascript to the bottom of the page solves the issue, but I'd like my script to work when that's not the case too. 将javascript移至页面底部即可解决此问题,但我希望我的脚本在情况并非如此时也能正常工作。

You want the DOMContentLoaded event, together with an external script linked after all of the stylesheets. 您需要DOMContentLoaded事件,以及在所有样式表之后链接的外部脚本。

From the linked page: 从链接页面:

if you have a <script> after a <link rel="stylesheet" ...> , the page will not finish parsing - and DOMContentLoaded will not fire - until the stylesheet is loaded. 如果在<link rel="stylesheet" ...> <script>之后有<script>在加载样式表之前,页面将无法完成解析-并且DOMContentLoaded将不会触发。

You could try to use a small image and add the handler to its onload event, see image.onload event and browser cache Add your code with this small image to document.ready. 您可以尝试使用一个小的图像,并将处理程序添加到其onload事件中,请参见image.onload事件和浏览器缓存将带有该小图像的代码添加到document.ready。

Please could you elaborate what is your use case, and why putting the code on document.ready event handler would not do the job for you? 请您能详细说明一下您的用例,为什么将代码放在document.ready事件处理程序上对您没有帮助?


Another option would be to attach handlers to onload of each of the style tags, and each of this handlers to increment some counter, which you would check at the same time if the counter = style tags count. 另一个选择是将处理程序附加到每个样式标签的加载上,并且每个此处理程序增加一些计数器,如果counter = style tags计数,则您将同时检查该计数器。 (You could determine the count of style tags, the same time you add the event handlers). (您可以在添加事件处理程序的同时确定样式标签的数量)。 When the counter has reached the number of style tags, you could fire your custom event, that could be used to have your code run. 当计数器达到样式标签的数量时,您可以触发您的自定义事件,该事件可用于运行代码。 This however would fail if some of the stylesheets fail to load for some reason, so I would suggest adding a fallback to execute the code on window.onload if the above logic fails. 但是,如果某些样式表由于某种原因无法加载,则此操作将失败,因此,如果上述逻辑失败,我建议添加一个后备代码以在window.onload上执行代码。

DOMContentLoaded is the first thing that is executed. 首先执行DOMContentLoaded

BUT

if i place an image like this in the html. 如果我将这样的图像放在html中。 and this image is cached then the image onload executes before DOMContentLoaded . 并且此图像被缓存,然后图像加载在DOMContentLoaded之前执行。

html html

<img onload="console.log('imgLoaded');" src="http://placekitten.com/16/16">

js js

window.addEventListener('DOMContentLoaded',function(){
 console.log('DOMContentLoaded')
},false);

DEMO 演示

http://jsfiddle.net/yE9qU/ http://jsfiddle.net/yE9qU/

output: 输出:

  • DOMContentLoaded DOMContentLoaded
  • imgLoaded img已加载
  • imgLoaded <----- wrong imgLoaded <-----错误
  • DOMContentLoaded DOMContentLoaded

also putting the onload event directly on the image does not work. 也无法将onload事件直接放在图像上。

http://jsfiddle.net/yE9qU/1/ http://jsfiddle.net/yE9qU/1/

output: 输出:

  • 10 10 10 10
  • 10 10 10 10
  • 16 16 <----- wrong 16 16 <-----错误
  • 10 10 10 10

This means that there is no way to get the right css assigned size before everything loads ... so after window.onload 这意味着在所有内容加载之前无法获得正确的CSS分配大小 ...因此在window.onload之后

Solutions... 解决方案...

depends on what you need. 取决于您的需求。

  1. if there are not to many images i would add them after executing load or DOMContentLoaded . 如果没有太多图像,我将在执行 loadDOMContentLoaded 之后添加它们

  2. like you say putting the script at the bottom of the page. 就像您说的那样将脚本放在页面底部。 but not so sure if image is already cached. 但不能确定图像是否已经缓存。

  3. a. 一种。 do the math after everything has loaded (it's just some milliseconds) 在所有内容加载后进行数学运算 (仅几毫秒)

  4. b. b。 hide the images until math is done. 隐藏图像,直到完成数学运算为止。

If you explain exactly why you need the size of the images in that exact moment it's easier for us to find you a proper alternative solution. 如果您确切地说明了为什么在那一刻需要图像尺寸的原因,那么我们很容易找到合适的替代解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM