是否可以在图像开始加载之前运行 javascript?
我想在浏览器请求图像之前更改图像的 src 属性,目的是使用 Timthumb 等 PHP 脚本减小图像的大小。使用 jQuery,我认为 $(document).ready
可以解决问题:
$(document).ready(function () {
var imgs = $('#container img');
jQuery.each(imgs, function() {
$(this).replaceWith('<img src="timthumb/timthumb.php?src=' + $(this).attr('src') + '&w=200" />');
});
});
但是原始的、未调整大小的图像仍然在后台下载到浏览器的缓存中。是否可以在客户端执行我想要执行的操作,或者服务器端是唯一的选择?
I'd like to change the src attribute of images before they are requested by the browser, the aim being to reduce the size of the images using a PHP script like Timthumb. Using jQuery, I thought $(document).ready
would do the trick:
$(document).ready(function () {
var imgs = $('#container img');
jQuery.each(imgs, function() {
$(this).replaceWith('<img src="timthumb/timthumb.php?src=' + $(this).attr('src') + '&w=200" />');
});
});
But the original, unresized image is still downloaded in the background to the browser's cache. Is it possible to do what I'm trying to do on the client side, or is server-side the only option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Javascript 加载和执行由浏览器序列化(除非您使用诸如
head.js
之类的东西),但问题是 DOM 必须可供脚本修改。 jQuery 就绪事件在 DOM 可用之后触发,因此浏览器已经开始请求 HTML 中引用的资源。因此,如果您将 Javascript 放在图像标签之前,它将无法找到图像标签,并且一旦准备就绪,下载就已经开始了。我不知道在图像加载之前触发的任何事件(只有一个中止事件),因此最干净的方法是首先使用修改后的 src 属性创建 HTML。
或者,将
src
放在图像上的不同属性中(例如data_orig_src
),然后运行脚本将src
设置为data_orig_src<文档准备好后,在每个图像上添加 /code>。在更改
src
之前使用 CSS 隐藏图像,这样用户就不会看到损坏的图像图标。我认为这可能比事后添加图像更好,因为您不需要跟踪图像需要放置在 DOM 中的位置,而且它的性能也应该更好。当然,如果您可以更改服务器以使用
data_orig_src
而不是src
,为什么不首先将正确的src
放在标记中...Javascript loading and execution is serialized by the browser (unless you use something like
head.js
), but the problem is that the DOM has to be available for a script to modify it. The jQuery ready event fires after the DOM is available, so the browser has already started requesting the resources that were referenced in the HTML.So if you put the Javascript before the image tag it won't be able to find the image tags, and once ready fires the download has already started. I'm not aware of any events that fire before image load (just one for aborts), so the cleanest method is to create the HTML with the modified
src
attributes in the first place.Alternatively, put the
src
in a different attribute on the image (likedata_orig_src
) and run the script to setsrc
todata_orig_src
on each image upon document ready. Use CSS to hide the images before changing thesrc
so the user doesn't see a broken image icon. I think this is probably better than adding the images after the fact because you won't need to track where the images need to be placed in the DOM, and it should perform better as well.Of course if you can change the server to use
data_orig_src
instead ofsrc
, why not just put the propersrc
in the tag in the first place...在 DOM 加载之前,您无法更改页面的 DOM。并且,一旦 DOM 被加载,浏览器就已经开始请求图像。因此,在
标签开始加载图像之前,您无法更改它们。
您可以做的就是更改页面源,使页面源中不包含任何您想要更改的图像,然后在页面加载后使用 javascript 动态插入所需的图像。这样浏览器就永远不会请求错误的图像。
或者,您可以将
标记更改为根本没有
.src
属性,并使用 Javascript 添加.src
> 财产。没有.src
属性的标记将不会显示,除非您为其提供
.src
属性。如果您担心在将图像更改为正确的图像之前加载时会闪烁错误的图像,则可以在样式表中使用 CSS 最初隐藏图像,然后在更改
img.src
到正确的值并且新的.src
值已加载,您可以使它们可见。如果您无法更改页面的源代码,那么您所能做的就是最初隐藏图像(使用 CSS),直到在它们上设置了正确的
.src
和新的。 src
值已加载。You cannot change the DOM of the page before the DOM has been loaded. And, once the DOM has been loaded, the browser has already started requesting images. So, you cannot change
<img>
tags before they start loading their images.What you could do is change the source of the page to not have any of the images you want to change in the source of the page and then use javascript to dynamically insert the desired images after the page has been loaded. This way the browser will never request the wrong images.
Or, you could change the
<img>
tags to not have a.src
property at all and with your Javascript you would add the.src
property. An<img>
tag with no.src
property will not display until you provide it with a.src
property.If you're worried about the wrong images flashing as they are loaded before you change them to the correct images, you can use CSS in a stylesheet to hide the images initially and then after you change the
img.src
to the correct value and that new.src
value has loaded, you can make them visible.If you can't change the source of the page, then all you can do is hide the images initially (using CSS) until the correct
.src
has been set on them and that new.src
value has been loaded.这是可能的,但不是以您当前拥有或可能想要的方式,并且它不会优雅地降级,但您可以从 html 中取出图像并使用 jQuery 将其插入到 html 中并应用您想要的任何更改。
示例:
但是这样做对我来说没有任何意义,为什么你不直接编辑图像源。
It is sort of possible but not in the way you currently have or probably want and it doesn't degrade gracefully but you can take the image out of your html and use jQuery to insert it into your html and apply whatever changes you want to it.
Example:
But doing it like this it doesn't make any sense to me as to why you wouldn't just edit the image source anyway.