最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript - Load an image onclick - Stack Overflow

programmeradmin4浏览0评论

OK, I'm actually calling some java middleware that returns a captcha image. The captcha image loads in the page on page load simply with:

<a href="#" id="ci"><img id="stickyImg" src="stickyImg" /></a>

I want to reload the captcha image onclick of the current captcha image. I've tried a few things but am not getting anything working.

By "tried a few things" I mean I have tried:

$("#ci").click(function(){

        $("#stickyImg").load('stickyImg');

        return false;
    });

which indeed loads the image but it does it by placing the raw binary image data inside the image tag so I get:

<img src="stickyImg"><img xmlns="">pngbinarygibberishsymbols</img>

Hmmm... maybe I need to specify putting the image into the src attribute? Perhaps? What do you all think?


EDIT:

Ugh! Putting the response into my img src results in:

<img src="    * captcha image�PNG  ��� IHDR�������Ketc, etc">

The raw binary data being output. What the heck?


SOLUTION:

The solution es from another, similar, post I made on this. You can read about it here.

In the end the (*edited thanks to Lee) code that works looks like:

$("#ci").click(function(){

        $("#stickyImg").attr('src', 'stickyImg?' + (new Date().getTime())); 
        return false;
    });

OK, I'm actually calling some java middleware that returns a captcha image. The captcha image loads in the page on page load simply with:

<a href="#" id="ci"><img id="stickyImg" src="stickyImg" /></a>

I want to reload the captcha image onclick of the current captcha image. I've tried a few things but am not getting anything working.

By "tried a few things" I mean I have tried:

$("#ci").click(function(){

        $("#stickyImg").load('stickyImg');

        return false;
    });

which indeed loads the image but it does it by placing the raw binary image data inside the image tag so I get:

<img src="stickyImg"><img xmlns="http://www.w3/1999/xhtml">pngbinarygibberishsymbols</img>

Hmmm... maybe I need to specify putting the image into the src attribute? Perhaps? What do you all think?


EDIT:

Ugh! Putting the response into my img src results in:

<img src="    * captcha image�PNG  ��� IHDR�������Ketc, etc">

The raw binary data being output. What the heck?


SOLUTION:

The solution es from another, similar, post I made on this. You can read about it here.

In the end the (*edited thanks to Lee) code that works looks like:

$("#ci").click(function(){

        $("#stickyImg").attr('src', 'stickyImg?' + (new Date().getTime())); 
        return false;
    });
Share edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Oct 31, 2010 at 1:38 LotharLothar 3,4998 gold badges46 silver badges58 bronze badges 3
  • where does your image located?? are you giving absolute path or some url like myimages./images/image.jpg – kobe Commented Oct 31, 2010 at 1:41
  • No absolute path. It's a java thing. The page load triggers "stickyImg" which summons the image from the java middleware that I am tied into. – Lothar Commented Oct 31, 2010 at 2:02
  • in your solution, the call to load('stickyImg') is unnecessary and could cause problems. you want just: $("#stickyImg").attr('src','stickyImg?'+(new Date().getTime())). – Lee Commented Oct 31, 2010 at 4:35
Add a ment  | 

2 Answers 2

Reset to default 3

it would be helpful if you would post a more plete example including the markup that embeds your image in the initial document; but making a few reasonable assumptions, I think you just need something like the following...

assuming that your markup is something like:

<div id="ci">
  <img src="/stickyImage" />
</div>

then the following JS should do the trick:

$("#ci").click(function(){

    $("img",this).remove();
    $(this).html('<img src="/stickyImage" />');

    return false;
});

good luck.


[edit] If you're using this code to load an image that's generated dynamically on your server (or that may change periodically, for some reason), then you'll also want to be sure that you properly account for the browser's tendency to cache data that it believes to be static. There are two way that this is monly acplished (either will work):

1) ensure that the url is always different every time the image is loaded. You can do this easily, by just appending a random number to the query string portion of the image url. So, in the above example, $(this).html('<img src="/stickyImage" />'), would bee $(this).html('<img src="/stickyImage?"+(new Date().getTime()) />'). (This is essentially identical to the approach that the OP ultimately settled on -- see OP's edits above).

2) ensure that the server returns the image data, including the proper HTTP headers in the response to indicate that the image is dynamic and shouldn't be cached. You can see more details about how to send no-cache headers on this SO post.

Here's how you would set the needed headers from within a Java servlet:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.

Option #2 is perhaps the more "technically correct" way to address the caching issue, but as I said above - either will work, and option#1 is a very reliable approach that is often substantially easier than option #2.

All this being said - the caching issue is a separate issue from the original question that was asked here. The original question involved display of binary blobs inline in the HTML. That problem resulted from the incorrect use of the jquery load() function. The OP settled on an approach that uses the attr() function to set the src attribute. The approach I've shown involves creating a new img element, and removing the old element. Either of these approaches will work, but the load() function will not work for this purpose.

this is a good post: http://emilsblog.lerch/2009/07/javascript-hacks-using-xhr-to-load.html

i referenced here: How do I load binary image data using Javascript and XMLHttpRequest?

发布评论

评论列表(0)

  1. 暂无评论