我希望能够区分来自当前域和外部域的图像请求,以处理画布和跨域请求。我目前正在代理所有绝对来源的图像,而不是使用
if ( $(this).attr('src').substr(0, 4) == 'http') { …
然而,这显然是低效的。区分当前域和其他域上的图像的最佳方法是什么?
发布于 2011-08-01 19:53:44
多亏了rsplak,Felix Kling和jammypeach,我才能做到这一点。我对其他人的解决方案是:
if (window.location.hostname !== this.src.split('/')[2]) { ...
只有当图像托管在解决问题的不同域上时,上面的语句才会被评估为真。
发布于 2011-08-01 18:43:49
您可以根据地址栏中的内容测试镜像src的主机名,如下所示:
var hostname = window.location.hostname;
$.each('img', function(){
if (hostname !== $(this).attr('src'))
{
$(this).attr('src', 'something-else');
}
});
这是假设jQuery。您可能需要调整hostname / img src或子字符串,以获得您想要测试的位。
进一步阅读:http://www.w3schools.com/jsref/obj_location.asp
https://stackoverflow.com/questions/6897211
复制相似问题