1

I wonder how it is possible that using javascript, images can be loaded asynchronously by changing the src of the img element after the page has been loaded. I thought that AJAX is for things like that(getting data from server without refreshing the page). Please clarify why it is working that way. The images are on server side, so I thought that i should refresh the page before the result will be visible.

Here is a sample code:

<!DOCTYPE html>
<html lang="en">
<head>
<script>
var photos = ["baloon", "game", "cliff"];
function changePhoto() {
    var input=document.getElementById("ph1");
    var iValue=input.value.trim();
    for(var tmp in photos) {
        if(photos[tmp] === iValue){
            var img=document.getElementById("photo");
            img.setAttribute("src", "img/"+iValue+".jpg");
        }
    }
}
</script>
</head>
<body>
  <input class="form-control" id="ph1" type="text" onkeyup="">
  <p>Photo: <span id="txtHint" onclick="changePhoto()"></span></p>
  </div>
  <div class="container" id="photocontainer">
    <img id="photo">
  </div>
</body>
</html>
Artyom Neustroev
  • 8,627
  • 5
  • 33
  • 57
ContentPenalty
  • 447
  • 1
  • 5
  • 14

3 Answers3

1

The user agent simply sends a GET request in response to the changing of the src attribute, the same that is done when a page loads initially.

AJAX is a technology that allows for asynchronous requests in JavaScript on the client. Browsers can make any requests they want at any time, as in this case, but without AJAX that couldn't be done in client-side code loaded by a website.

For example, I just changed the src property of an element in a page through Chrome Developer Tools and watched the GET request execute.

enter image description here

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
  • But its going asynchronously, the page isn't reloading, so what is the purpose of ajax then?. – ContentPenalty Oct 15 '15 at 19:56
  • @user1816806 That sounds like a good topic for a [new question](http://stackoverflow.com/questions/ask) :) But basically, AJAX is a technology to allow you to make asynchronous requests in _JavaScript_ on the client. Browsers can make any requests they want at any time, but without AJAX that couldn't be done in client-side code loaded by a website. – Drew Gaynor Oct 15 '15 at 19:58
  • @user1816806 I've also updated my answer with a little more explanation. – Drew Gaynor Oct 15 '15 at 19:59
  • @ user1816806 Ajax is used for async communication between client and server. You can send some stringified objects etc or another data using it. But there is no need to send images through ajax. – Yuriy Yakym Oct 15 '15 at 20:06
  • What is confusing me is: "_AJAX (Asynchronous Javascript XML) is a method to dynamically update parts of the UI **without having to reload the page**_". But here the page wasn't reloaded also and there is no ajax. – ContentPenalty Oct 15 '15 at 21:20
  • @user1816806 Sure, AJAX is **one** method that dynamically updates parts of the UI. It is not the only way that resources can be retrieved without reloading the page. – Drew Gaynor Oct 16 '15 at 02:28
0

What you're doing in your code is not Ajax. Is simple javascript.

To make this work with Ajax you need a server side aplication that actually renders the image or get the contents from an existing file, and after that you show your loaded file on the browser.

Look for a jQuery ajax calls. On the return of the call you can put your code. Like this:

$.ajax({
    url: "SomeUrl/SomeMethod/"
})
.done(function (response) {
    //Do stuff here with the response to show the image
});
King Clauber
  • 39
  • 10
0

When you change src atrribute of an img element browser automatically starts downloading this image asynchronously.

Your code is almost ok. You iterate through array incorrectly. You can try doing it this way instead:

var photos = ["baloon", "game", "cliff"];
function changePhoto() {
    var input=document.getElementById("ph1");
    var iValue=input.value.trim();

    if(photos.indexOf(iValue) > -1) {
        var img=document.getElementById("photo");
        img.setAttribute("src", "img/"+iValue+".jpg");
    }
}

Iterating through array

You can iterate through array for example like this:

for(var i=0; i<photos.length; i++) {
    var photo = photos[i];
    //...
}

or like this:

for(var k in Object.keys(photos)) {
    var photo = photos[k];
    //...
}
Yuriy Yakym
  • 3,616
  • 17
  • 30