I was wondering. When we change the imagesrc onclick from ex. “images/myimage1.jpg” to “images/myimage2.jpg” does this somehow make a trip back to the server to get the image or how does it really work? I Was wondering because it doesn’t cause a pagereload when the image changes, and the image that we are changing to is not preloaded. Everything works fine, just curious how it works behind the scenes when changing src.
We are using these kind of “methods” alot, not just for images. Srcipts, css and so on…
Is the script/image/whatever loaded in the sources? No? Then we have to communicate with the server to get it. Just because the page has completed its initial load doesnt mean it cant make additional requests to the server.
Well it doesnt strictly use AJAX, because it’s not Javascript doing the loading - it’s the browser. It’s asynchronous in the sense that your script doesn’t wait around for it to load before continuing (because all the script did was change an attribute, it doesnt know if the browser already has the image to hand or not)
Yes. But it doesn’t make a new request to the server. Its like the browser is talking to the server from distance and telling the server to meet him up with the images, instead of going all the way to the server himself
So thats good. But how can I preload the images to the source as you mentioned and then be sure that if I change the src of any element, it looks directly within “what we already have recived” so it does not make unnecessary checks or new trips to other places?
Well, it will always check to see if it has the image. It cant… not-check. And it always ‘makes a trip’ to the server to get the image if it doesn’t have it.
When you load a webpage, the browser doesnt make a single ‘trip’ to the server. It makes many trips, to potentially many servers.
Browser: “I need index.html, YourServer”
YourServer: “Okay, here it is.”
Browser: “In here I see I need image1.jpg from YourServer.”
YourServer: “Okay, here it is.”
Browser: “In here I see I need image2.jpg from YourServer.”
YourServer: “Okay, here it is.”
Browser: “I need jquery.js from Google’s CDN.”
Google CDN: “Okay, here it is.”
etc etc
You can see this in the Network window of the developer tools of your browser. For example, I opened google.com.
Is it possible to get multiple srcfiles returned from Ajaxcall instead of only one "text"response ? So if I check the source after ajaxcall there would be newfiles available, scripts, images, whatever I want