javascript如果图像不存在don't加载它
javascript if image does not exist don't load it
我有一个javascript代码,可以将图像加载到popwindwindow中。我需要首先检查标签src中是否有图像。如果存在,添加
popupContent += '<tr><td><img src="'+mypath+'"/></td></tr>';
否则什么都不做
var mypath = "whatever here but loaded on an ajax get call after hitting a button";
var popupContent = '<table>';
popupContent += '<tr><td>Other fields</td></tr>';
popupContent += '<tr><td><img src="'+mypath+'"/></td></tr>';
popupContent += '</table>';
试试这样的东西:
function checkImageExists(imagePath){
var httpReq = new XMLHttpRequest();
httpReq.open('HEAD', imagePath, false);
httpReq.send();
return httpReq.status != 404;
}
我已经修改了您的代码并添加了检查。
var mypath = "whatever here but loaded on an ajax get call after hitting a button";
var popupContent = '<table>';
popupContent += '<tr><td>Other fields</td></tr>';
if (mypath) {
popupContent += '<tr><td><img src="'+mypath+'"/></td></tr>';
}
popupContent += '</table>';
试试这样的方法:如果有图像,它会显示图像,否则会显示No image
。
var mypath = "whatever here but loaded on an ajax get call after hitting a button";
var hasImg = mypath ? true : false;
var popupContent = '<table>';
popupContent += '<tr><td>Other fields</td></tr>';
hasImg ? popupContent += '<tr><td><img src="'+mypath+'"/></td></tr>' : popupContent += '<tr><td><span>No image</span></td></tr>';
popupContent += '</table>';
相关文章:
- javascript如果图像不存在don't加载它
- $(select).selectmenu('打开')不'如果使用ajax加载页面,则不起作用
- 如果数据为空,Ajax加载不会停止
- 如果图像标记是用javascript编写的,并且需要用户输入,那么图像不会随页面加载吗
- 如何知道用户已经重新加载了网页,使用jquery.如果用户重新加载网页,
- 在HTML中,如果第二次加载相同的脚本文件,它还会被加载吗
- 如何让我的网站上的WEBP图像加载在morzilla firefox中有一个可能的解决方案吗?如果是,那么如何
- Javascript:Google Maps API:如果地图加载在隐藏容器中,则为空白地图
- 如果iframe通过单击加载,则自动调整iframe大小无效
- 如果使用jquery在页面加载时未选择任何选项,如何禁用下拉列表
- php,如果我点击按钮程序重新加载页面,并且从不继续执行代码
- 如果可能的话,使用jQuery onLoad函数来加载特定的页面
- 如果页面加载时间超过 10 秒,如何刷新页面
- 在页面加载时显示 css(如果已选中)
- 如果有办法在 N 秒内加载未触发,则有没有办法设置 img src
- 我能确定这个页面赢了吗;如果我使用`location.replace`来更改哈希,则不会重新加载
- jQuery-检查外部页面上是否存在DIV,如果存在,则加载DIV,否则加载另一个
- 检查IFRAME URL并重新加载(如果找到)-Javascript
- 检查脚本是否已经加载.如果没有,则异步加载
- onkeypress事件的奇怪行为页面重新加载如果"返回false;"是缺失的,否则它不会让我输入任