Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Design ›
Javascript Reload Image

Javascript Reload Image

January 22, 2011 Web Design Jesin A 3 Comments

web design category thumbnail

Using Javascript you can reload an image without reloading the page. This is extremely useful for reloading a captcha image if the user is unable to read the characters shown in it. Reloading an image using Javascript is just a simple matter of reassigning the same image location to the image src attribute. But the problem is the browser doesn’t call the image from the web server, since the name is same the browser just fetches the same image from the cache (Temporary Internet Files). So to overcome this we must add something with the image src attribute in such a way that it doesn’t affect the image location. The best way to do it is to add it like a query string to the image.But what do we add ? We can generate random numbers or alphanumeric characters and append as a query string. But if incase the same random string is generated when a user presses the reload button the image will not be reloaded from the server. So the best way to make sure the value is unique each and every time is to use the function getTime() this function returns milliseconds since 1970/01/01 so this value will be definitely unique each time the user clicks the reload button unless clicking is done twice in a millisecond !!! Place the following code inside either the <head> tag or <body> tag. But before that set the id attribute of that image to imageid or if you modify it do that same to the inside getElementById()

<script type="text/javascript">
function reloadImg() {
var d=new Date();
document.getElementById("imageid").src="captcha_image.php?a="+d.getTime();
}
</script>

The function must be called through a link or button. For calling it through a link

<a href="javascript:reloadImg()">Reload Image</a>

For calling the same through a button

<input type="button" value="Reload Image" onclick="reloadImg()" />

Try a live demo with a captcha image.

Related posts:

web design category thumbnailJavascript Simple Slideshow web design category thumbnailJavascript Validation with Regular Expressions web design category thumbnailJavascript Validation – Textbox Combobox Radiobutton Checkbox php category thumbnailPHP Captcha Tutorial php category thumbnailPHP random string generation

Tags: html, javascript

Comments

  1. Andy says

    July 16, 2011 at 12:50 am

    Works perfectly. Thanks!

    Reply
  2. Deepak says

    August 30, 2016 at 5:00 pm

    help ful..

    Reply

Trackbacks

  1. PHP Captcha Tutorial | Jesin's Blog says:
    June 29, 2013 at 11:58 pm

    […] Home › PHP › PHP Captcha Tutorial← PHP random string generationJavascript Reload Image → […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.