简体   繁体   English

jquery文件准备好的监听器与图像加载; 执行顺序

[英]jquery document ready listener vs image loading; execution order

I have a page where I want to display different images based on the time of day. 我有一个页面,我想根据一天中的时间显示不同的图像。

How I have this working now is that attached to the jquery ready listener I have a function that selects all image elements and changes their source based on the hour. 我现在如何工作是附加到jquery ready listener我有一个函数,它选择所有图像元素并根据小时更改它们的源。 Each img tag has a default src specified in the html, just in case the user has js deactivated or whatever. 每个img标签都有一个在html中指定的默认src,以防用户已停用js或其他任何内容。

With this method, however, I am paranoid that the browser starts loading the default image, then proceeds to load the time-specific image, causing unnecessary overhead. 但是,使用这种方法,我很偏执,浏览器开始加载默认图像,然后继续加载特定于时间的图像,从而导致不必要的开销。

So my question is this: if I change the img source in the jquery ready event, does the img src get changed before the browser starts loading images. 所以我的问题是:如果我在jquery ready事件中更改了img源代码,那么在浏览器开始加载图像之前img src是否会被更改。

Thanks for any and all responses. 感谢任何和所有回复。 I apologize if this is a duplicate question, but there are a million questions regarding the nuances of the jquery ready event. 如果这是一个重复的问题,我很抱歉,但有关jquery ready事件的细微差别有一百万个问题。

I think the ready listener starts running as soon as all the html loads, meaning it will not wait for bg to load and ready will run. 我认为就绪监听器会在所有html加载后立即开始运行,这意味着它不会等待bg加载并准备运行。 So depends on timing you can have default loading then getting switched. 所以取决于你可以有默认加载然后切换的时间。

Perhaps you can leave out the src for the image or the css for the bg and only add it with jquery in ready(). 也许你可以省略图像的src或bg的css,只在ready()中添加jquery。

For people disabling js, you can have the default load in a noscript block 对于禁用js的人,可以在noscript块中使用默认加载

You can find the answer to this right in jQuery's documentation : 你可以在jQuery的文档中找到这个权利的答案:

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. 虽然JavaScript提供了在呈现页面时执行代码的加载事件,但在完全接收到所有资产(如图像)之前,不会触发此事件。 In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. 在大多数情况下,只要完全构造DOM层次结构,就可以运行脚本。

So once the code is there, the ready event starts running regardless of the load status of the images. 因此,一旦代码存在,无论图像的加载状态如何,就绪事件都会开始运行。 This means that the image may or may not be displaying, it may be fully or partially loaded, it just depends on how quickly the image loads before the command to change it is to received. 这意味着图像可能正在显示,也可能没有显示,可能是完全或部分加载,它只取决于图像在接收更改命令之前加载的速度。

If you wanted to be absolutely positively sure that the image never loaded, you would want to do this server-side so that only the image you wanted got written to the code--if there were some delay in the execution of the change, you would definitely see all or part of the default image. 如果你想绝对肯定图像永远不会被加载,你会想要做这个服务器端,这样只有你想要的图像被写入代码 - 如果执行更改有一些延迟,你肯定会看到全部或部分默认图像。

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

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