简体   繁体   English

如何验证URL中的内容(Javascript)

[英]How to validate a content in URL (Javascript)

I have URLs in JSON feed n each URL contains one image. 我在JSON feed中有URL,每个URL包含一个图像。 I want to validate those URLs which have image or not (NULL). 我想验证那些有图像或没有图像的URL(NULL)。 How to validate this using javascript. 如何使用javascript验证这一点。 since I'm a beginner, i don't know, how to validate this. 因为我是初学者,我不知道,如何验证这一点。 thanks 谢谢

      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",

        },

Interesting question! 有趣的问题!

Try to add the images to a div (use jQuery, the div can be hidden using display:none; ). 尝试将图像添加到div(使用jQuery,可以使用display:none;隐藏div)。
Then test the properties of the images regularly (use a setInterval loop) whether they indicate a successful load. 然后定期测试图像的属性(使用setInterval循环),无论它们是否表示成功加载。 Define some timeout after which the setInterval will kill itself. 定义一些超时,之后setInterval将自行终止。

This property works for testing successful img load in Chrome and Firefox: 此属性适用于在Chrome和Firefox中测试成功的img加载:

naturalHeight != 0

This property works for testing successful img load in IE7,8,9: 此属性适用于在IE7,8,9中测试成功的img加载:

fileCreatedDate != ''

Test both to make it work in all major browsers. 测试两者以使其适用于所有主流浏览器。

Here is a fiddle to see how the props change in 10ms steps during the load process. 这是一个小提琴,看看道具在加载过程中如何以10毫秒的步长变化。 Try it in different browsers!: 在不同的浏览器中尝试!:
http://jsfiddle.net/xGmdP/ http://jsfiddle.net/xGmdP/

In Chrome and FF you can even replace the timeout for the setInterval loop by checking the complete property of all images. 在Chrome和FF中,您甚至可以通过检查所有图像的完整属性来替换setInterval循环的超时。 But unfortunately this does not work in IE ( complete always stays false in IE for images that cannot be loaded). 但不幸的是,这在IE中不起作用(对于无法加载的图像,IE中的完全始终保持错误)。

Finally here is the complete code of the fiddle: 最后这里是小提琴的完整代码:

<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>

If you want to check if the string is null just do if(previewImage == "") { /* image is null, do stuff */ } but if you want to check if the image actually exists I would suggest doing a GET request via AJAX and check the HTTP response, eg: (jQuery): 如果你想检查字符串是否为空,只需执行if(previewImage == "") { /* image is null, do stuff */ }但如果要检查图像是否实际存在,我建议执行GET请求通过AJAX并检查HTTP响应,例如:(jQuery):

$.ajax({
    url: "http://123.201.137.238:6060/feed/videoplay/Jackie.png",
    cache: false,
    error: function(){
       alert("image doesn't exist!");
    }
});

But it would be "nicer" to do this server side if possible. 但如果可能的话,做这个服务器端会更“好”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM