views:

33

answers:

2

I need to delete or hide the Img tag or its parent when Img have src="".

I have a script but it is not working..

window.onload=function(){ var imgs = document.getElementsByTagName("img"); for(var i = 0; i < imgs.length; i++) { var img = imgs[i]; if(img.src==""){ img.parentNode.removeChild(img); } } }

Its is not finding the img having scr="". while in IE and Safari is is showing cross sign ( Red ).

Can anybody help.

Thanks.

+1  A: 

You can use jquery to add in functionality that will make this cross-browser compliant without going bonkers. Your code would end up looking like

$(document).ready(function() {
    $("img[src='']").remove();
});
C Bauer
Anyone want to explain the downvote?
C Bauer
Not the one that downvoted, but while jQuery may be javascript, javascript isnt jQuery.
Daniel
Everyone uses jQuery for everything these days. Not that I don't like jQuery myself, but this indicates something is seriously wrong with both the way JS is implemented (cross-browser compliance!?) and the feature set of client-side JS. Too many simple tasks need a 24,000 character (guess) library. Pushing all that functionality into client-side JS would be as much work as all the JS interpreter optimalization they're doing now, but would result in less traffic...
MvanGeest
Thanx for reply.. i need to put it on on ready event.. m i right
Anil
So I got a downvote because of how poorly javascript is implemented in browsers?
C Bauer
thanxx guys.....
Anil
You already selected an answer, did that other guy not answer the question? Then you need to uncheck it or we don't know to keep helping you.
C Bauer
I just checked my answer above, assuming that your empty src img tags actually look like <img src=''> then it works, but if your src does not exist at all it would need something else. Yes, you do need the document.ready() stuff.
C Bauer
@@ C Bauer.... Thanks for helping me. I chose that as answer because I implemented above solution and it gave what i exactly wanted. I am not saying that your answer is wrong. but i edited my question at the end.
Anil
@Anil - Sorry I think that elipsis (...) mean something different where I come from, I thought you were indicating that you still needed help on this issue when you said "thanxx guys.....". Cheers!
C Bauer
@C Bauer.. actually Thanks Bro..
Anil
A: 

You can user jQuery for that :)

$(document).ready(function(){
    $("img").each(function(){
        if($(this).attr("src") == null || $(this).attr("src") == '')
         {
             $(this).remove();
          }
    });
});

The workaround is set the style of the div container where there is the news as display:none; then once th for each loop is executed you set the div as visible.

here you have a code mockup..

 //html (iframe content)
 <div id="newsContainer" style="display:none;">
 </div>

$(document).ready(function(){
    $("img").each(function(){
        if($(this).attr("src") == null || $(this).attr("src") == '')
         {
             $(this).remove();
          }
    });
    $("#newsContainer").show();
 });
David Bonnici
thanx for reply.. i m implementing this
Anil
no problem.. tick it as answered :)
David Bonnici
Hey David.. thanx for reply. I worked for me. I will mark this as answer. But i need more specific answer. I have an .asp and i have iframe in this and this iframe is populated with news and images. Iframe is using a html page. I put this code on html page. but scene is firstly IE is showing RED cross sign and after all the page has laoded, it removes img tag. I applied the my script on <img> tags onload event, but it didnot worked. any help on this.
Anil
i need browser never show red sign.
Anil