有一个需求是这这样的,当页面加载完的时候,根据当前页面的URL,来判断对应的图片资源是否存在,如果存在则就用这个对应的图片,如果不存在则使用默认的图片。假设当前页面URL:https://www.example.com/somep...规则对应的图片文件是someid.png,图片的URL:https://imgs.example.com/imgs...如果不存在这张图片,则使用默认的https://imgs.example.com/imgs...这里的jquery如何去判断https://imgs.example.com/imgs...?varpopup=(function(){vartarget=window.location.href;varcurrentFilename=window.location.pathname.split("/")[window.location.pathname.split("/").length-1].split(".")[0];varcurrentPopupImg='https://imgs.example.com/'+currentFilename+'.png';vardefautlPopupImg='https://imgs.example.com/default.png';varpicContent="";varcheckPopupImgOnce=function(){$.ajax({url:currentPopupImg,cache:false,dataType:"jsonp",type:'GET',complete:function(response){if(response.status==200){picContent=currentFilename;}else{changePopUpImgSrc();}}});}varchangePopUpImgSrc=function(){picContent=defautlPopupImg;}varshowPopUp=function(){varflag=0;layer.open({type:1,title:false,closeBtn:0,shade:0,shadeClose:true,anim:0,skin:'popup',area:['600px','450px'],time:12000,resize:false,btn:['不再提醒','稍后再说','立即了解'],yes:function(index,layero){//按钮【按钮一】的回调//永远关闭弹窗flag=1;layer.closeAll();},btn2:function(index,layero){//按钮【按钮二】的回调//$("#layui-layer1").hide();//暂时关闭弹窗flag=0;layer.close(index);},btn3:function(index,layero){//按钮【按钮三】的回调//returnfalse开启该代码可禁止点击该按钮关闭//打开快商通//暂时关闭弹窗flag=0;window.open('https://ad.example.com');layer.close(index);},btnAlign:'c',content:picContent,end:function(){if(flag==1){//永远关闭弹窗window.clearTimeout(timer);}else{timer=setTimeout(popup,15000);}}});}varpop={checkimg:checkPopupImgOnce,showPopUp:showPopUp}returnpop;})();popup.checkimg();popup.showPopUp();
2 回答
至尊宝的传说
TA贡献1789条经验 获得超10个赞
利用标签,大致思路:functionisValidImage(url,successCallback,abortCallback,errorCallback){varimg=document.createElement('img');img.src=urlimg.onload=successCallbackimg.onabort=abortCallbackimg.onerror=errorCallbackreturnimg}
添加回答
举报
0/150
提交
取消