7

I have a string of HTML that I use as jQuery input document.

// the variable html contains the HTML code

jQuery( html ).find( 'p' ).each( function( index, element ) {
    // do stuff
});

Though, I keep getting errors that the images doesn't exist (in my variable html). The images in the HTML have relative URL's which doesn't correspond to images on my host, so naturally, they can't be found so I get 404 errors in my console.

Is there a jQuery-way to avoid jQuery loading the images? If not, I'll have to find all images and replace the src using non-jQuery which is a bit sad because that's exactly where jQuery comes in handy :p


Edit:

I'll explain step by step.

I have a variable, html, that contains some HTML code from another site. I put that variable in the jQuery constructor, because I want to perform actions on that HTML.

jQuery( html );

At this point I get 404 errors because the images in the HTML source are relative; they don't correspond to the images I have on my host.

  • I can't use jQuery to remove the src tag, because I still get errors
  • Writing a bit of javascript to modify the src value is plain silly; this is why I use jQuery

So, again, all I'm asking is if there's a setting or whatever that avoids jQuery from loading the images in the supplied source.

Thank you.

Tim S.
  • 13,597
  • 7
  • 46
  • 72
  • understand you. you need to replace SRCs from the document before creating new jquery object. ckeck code at my answer – abrahab May 15 '12 at 12:17

2 Answers2

0

right after you get the html - replace the src to something else ()maybe to your own picture [global url]. ( simple string replace)

this will help you not to get the 404 - not found error in the console.

also you can make the html as jQuery - and set all the $("img") with your SRC or remove it if you want.

tell me if thats what you after.

edit

what about

$("img").removeAttr("src");

edit2

    string html = @"
<h1>
<img src="" ... >
</img>
<img></img>-bad
<img/>-bad
<img src="" ... />
</h1>";
            string result = Regex.Replace(html, @"<img\s[^>]*/>", "", RegexOptions.IgnoreCase);
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • I don't think you understand my question. jQuery is loading the images it finds in the inputted source code, so I get 404 errors. I'm asking if there's a jQuery-way to prevent images being loaded by jQuery. – Tim S. May 15 '12 at 11:50
  • youll have to edit - as ive said - the html . jquery cant just make a switch which stops img from loading thie src. – Royi Namir May 15 '12 at 11:51
  • When I put the HTML code in the jQuery constructor, jQuery seems to verify all images. That's where I get the errors, so I can't use jQuery to remove the attribute because by that time the images are already being verified and returning a 404. If I have to edit the HTML, I have to use javascript which is silly because that's where jQuery is for. So again, I'm asking if there's a jQuery method or setting that prevents jQuery from loading images of the supplied HTML code. – Tim S. May 15 '12 at 11:59
  • ok so dont put it in the jquery constructor - treat it as its a string. see my edit 2 – Royi Namir May 15 '12 at 12:12
0

To stop images loading you need to modify html before creating jQuery( html ) object. You can use jquery for that also.

var newhtml = html.replace('src=','nosrc=');
jQuery( newhtml ).find( 'p' ).each( function( index, element ) {
    // do stuff
});
abrahab
  • 2,430
  • 9
  • 39
  • 64