如何验证URL中的内容(Javascript)
我在JSON提要中有URL,每个URL包含一个图像。我想验证那些有图像或没有图像的URL(NULL)。如何使用javascript验证这一点。 因为我是一个初学者,我不知道,如何验证这一点。谢谢如何验证URL中的内容(Javascript),javascript,json,url,null,validation,Javascript,Json,Url,Null,Validation,我在JSON提要中有URL,每个URL包含一个图像。我想验证那些有图像或没有图像的URL(NULL)。如何使用javascript验证这一点。 因为我是一个初学者,我不知道,如何验证这一点。谢谢 JSON feed with image n NULL { previewImage:"http://123.201.137.238:6060/feed/videoplay/Jackie.png", }, {
JSON feed with image n NULL
{
previewImage:"http://123.201.137.238:6060/feed/videoplay/Jackie.png",
},
{
previewImage:"http://www.snovabits.net/feed/ImageMI/WarCraft.jpg",
},
如果要检查字符串是否为null,只需执行
如果(previewImage==“”){/*图像为null,则执行stuff*/}
但如果要检查图像是否确实存在,我建议通过AJAX执行GET请求并检查HTTP响应,例如:(jQuery):
但如果可能的话,在服务器端这样做会“更好”。有趣的问题
尝试将图像添加到div(使用jQuery,可以使用display:none;隐藏div)。然后定期测试图像的属性(使用setInterval循环)是否表示加载成功。定义一个超时,在此超时之后,setInterval将自动终止 此属性用于在Chrome和Firefox中测试成功的img加载:
naturalHeight != 0
该特性用于测试IE7,8,9中的成功img负载:
fileCreatedDate != ''
测试两者,使其在所有主要浏览器中都能工作
这里有一把小提琴,看看在加载过程中道具是如何以10毫秒的步幅变化的。在不同的浏览器中尝试它!:在Chrome和FF中,您甚至可以通过检查所有图像的完整属性来替换setInterval循环的超时。但不幸的是,这在IE中不起作用(对于无法加载的图像,complete在IE中始终保持为false) 最后,这里是小提琴的完整代码:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
$(function(){
$('#images').prepend('<img id="img1" src="http://123.201.137.238:6060/feed/videoplay/Jackie.png">')
$('#images').prepend('<img id="img2" src="http://www.snovabits.net/feed/ImageMI/WarCraft.jpg">')
setInterval(test, 10);
})
function test(){
var props=['naturalHeight', 'complete', 'fileCreatedDate', 'clientHeight', 'offsetHeight', 'nameProp', 'sourceIndex'];
for(i in props){
var pr = props[i];
$('#testresults').append('1:'+pr+' = ' + $('#img1').prop(pr)+'<br>');
$('#testresults').append('2:'+pr+' = ' + $('#img2').prop(pr)+'<br>');
}
$('#testresults').append('---<br>');
}
</script>
</head>
<body>
<div id="images"><div>
<div id="testresults"></div>
</body>
</html>
$(函数(){
$(“#图像”)。前缀(“”)
$(“#图像”)。前缀(“”)
设定间隔(试验,10);
})
功能测试(){
var props=['naturalHeight'、'complete'、'fileCreatedDate'、'clientHeight'、'offsetHeight'、'nameProp'、'sourceIndex'];
对于(我在道具中){
var pr=道具[i];
$('#testresults').append('1:'+pr+'='+$('#img1').prop(pr)+'
');
$('#testresults').append('2:'+pr+'='+$('#img2').prop(pr)+'
');
}
$('#testresults')。追加('-
');
}
您是否可以发布示例json,最好带有其他人可以访问的链接。另外,发布您迄今为止编写/尝试过的代码。除非所有图像都与您的JavaScript托管在同一个域中,否则您无法单独使用JavaScript执行此操作。是这样吗?还有别的办法吗?做这个好主意,但对我不起作用。虽然图像存在,但会触发警报。哇!伟大的我正期待着这个答案。非常感谢您的回复。
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
$(function(){
$('#images').prepend('<img id="img1" src="http://123.201.137.238:6060/feed/videoplay/Jackie.png">')
$('#images').prepend('<img id="img2" src="http://www.snovabits.net/feed/ImageMI/WarCraft.jpg">')
setInterval(test, 10);
})
function test(){
var props=['naturalHeight', 'complete', 'fileCreatedDate', 'clientHeight', 'offsetHeight', 'nameProp', 'sourceIndex'];
for(i in props){
var pr = props[i];
$('#testresults').append('1:'+pr+' = ' + $('#img1').prop(pr)+'<br>');
$('#testresults').append('2:'+pr+' = ' + $('#img2').prop(pr)+'<br>');
}
$('#testresults').append('---<br>');
}
</script>
</head>
<body>
<div id="images"><div>
<div id="testresults"></div>
</body>
</html>