有一個需求是這這樣的,當(dāng)頁面加載完的時候,根據(jù)當(dāng)前頁面的URL,來判斷對應(yīng)的圖片資源是否存在,如果存在則就用這個對應(yīng)的圖片,如果不存在則使用默認(rèn)的圖片。假設(shè)當(dāng)前頁面URL:https://www.example.com/somep...規(guī)則對應(yīng)的圖片文件是someid.png,圖片的URL:https://imgs.example.com/imgs...如果不存在這張圖片,則使用默認(rèn)的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){//按鈕【按鈕一】的回調(diào)//永遠(yuǎn)關(guān)閉彈窗flag=1;layer.closeAll();},btn2:function(index,layero){//按鈕【按鈕二】的回調(diào)//$("#layui-layer1").hide();//暫時關(guān)閉彈窗flag=0;layer.close(index);},btn3:function(index,layero){//按鈕【按鈕三】的回調(diào)//returnfalse開啟該代碼可禁止點擊該按鈕關(guān)閉//打開快商通//暫時關(guān)閉彈窗flag=0;window.open('https://ad.example.com');layer.close(index);},btnAlign:'c',content:picContent,end:function(){if(flag==1){//永遠(yuǎn)關(guān)閉彈窗window.clearTimeout(timer);}else{timer=setTimeout(popup,15000);}}});}varpop={checkimg:checkPopupImgOnce,showPopUp:showPopUp}returnpop;})();popup.checkimg();popup.showPopUp();
JQ怎么跨域判斷HTTPS的URL資源是否有效?
qq_笑_17
2019-05-22 23:01:50