Javascript 如何从这种链接中获取图像类型?

Javascript 如何从这种链接中获取图像类型?,javascript,jquery,Javascript,Jquery,我的问题是如何从这种链接中获取图像类型 例如,在如下HTML中: <img src="https://www.gravatar.com/avatar/71b0d516013802a2d67aeb7c2e77ed32?s=48&amp;d=identicon&amp;r=PG&amp;f=1" alt="" width="24" height="24" class="-avatar js-avatar-me"> 对于类似于something.jpg?name=

我的问题是如何从这种链接中获取图像类型

例如,在如下HTML中:

<img src="https://www.gravatar.com/avatar/71b0d516013802a2d67aeb7c2e77ed32?s=48&amp;d=identicon&amp;r=PG&amp;f=1" alt="" width="24" height="24" class="-avatar js-avatar-me">
对于类似于something.jpg?name=blah或something.jpgblah的URL,我使用了:

url.split('.').pop().split(/\#|\?/)[0]; 

获取链接。

由于内容是动态生成的,并且没有文件扩展名,因此无法使用拆分来解析URL。即使这样,依赖文件名也不准确,因为您可以简单地将任何文件重命名为.jpg。它仍然可以是应用程序/文本文件或任何其他MIME类型

相反,您可以向图像的位置发出AJAX请求,并从响应中检索内容类型头,如下所示:

$.ajax{ 网址:'https://www.gravatar.com/avatar/71b0d516013802a2d67aeb7c2e77ed32?s=48&;d=识别图标&;r=PG&;f=1', success:functor,s,x{ var type=x.getResponseHeadercontent-type; console.logtype; } };
由于内容是动态生成的,并且没有文件扩展名,因此无法使用拆分来解析URL。即使这样,依赖文件名也不准确,因为您可以简单地将任何文件重命名为.jpg。它仍然可以是应用程序/文本文件或任何其他MIME类型

相反,您可以向图像的位置发出AJAX请求,并从响应中检索内容类型头,如下所示:

$.ajax{ 网址:'https://www.gravatar.com/avatar/71b0d516013802a2d67aeb7c2e77ed32?s=48&;d=识别图标&;r=PG&;f=1', success:functor,s,x{ var type=x.getResponseHeadercontent-type; console.logtype; } };
如何使用纯javascript实现同样的功能@Rory McCrossann如何使用纯javascript实现同样的功能@罗里·麦克罗森
url.split('.').pop().split(/\#|\?/)[0];