图像 src(通过 javascript)chrome/firefox 之间不一致

发布于 2024-10-07 03:28:26 字数 339 浏览 0 评论 0原文

嘿,所以我遇到了一个奇怪的错误/边缘情况。查看以下代码:

var i = new Image();
i.src = 'http://ia.media-imdb.com/images/M/MV5BMTIxOTAxNTc4NF5BMl5BanBnXkFtZTcwOTg1NzQyMQ@@._V1._SX97_SY140_.jpg';
console.log(i.width);

这在 chrome 中工作正常(因为它指的是真实图像),但在 FF 中失败。认为这可能与“at”符号或双扩展名有关(例如“._V1._SX97_SY140_.jpg”),但并不真正知道。

感谢您的任何帮助。

Heya, so I'm running into a weird bug/edgecase. Check out the following code:

var i = new Image();
i.src = 'http://ia.media-imdb.com/images/M/MV5BMTIxOTAxNTc4NF5BMl5BanBnXkFtZTcwOTg1NzQyMQ@@._V1._SX97_SY140_.jpg';
console.log(i.width);

This works fine in chrome (as it's referring to a real image), but fails in FF. Thought it may have to do with the 'at' signs or the double extension (eg. '._V1._SX97_SY140_.jpg'), but don't really know.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-10-14 03:28:26

请记住,图像是异步加载的。您需要为图像的加载事件分配一个事件处理程序并在那里获取宽度:

var i = new Image();
i.onload = function() {
  console.log(this.width);
}
i.src = 'http://ia.media-imdb.com/images/M/MV5BMTIxOTAxNTc4NF5BMl5BanBnXkFtZTcwOTg1NzQyMQ@@._V1._SX97_SY140_.jpg';

keep in mind, that the image is loaded asynchronously. You need to assign a event handler for the load event of the image and get the width there:

var i = new Image();
i.onload = function() {
  console.log(this.width);
}
i.src = 'http://ia.media-imdb.com/images/M/MV5BMTIxOTAxNTc4NF5BMl5BanBnXkFtZTcwOTg1NzQyMQ@@._V1._SX97_SY140_.jpg';
几味少女 2024-10-14 03:28:26

403 响应标头意味着 禁止 (wiki),您不是允许访问该资源。

imdb.com 这样做可能是为了防止其图像在其他网站上被热链接。

A 403 response header means Forbidden (wiki), that you are not allowed to access the resource.

imdb.com might be doing this to prevent hotlinking of their images in other sites.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文