I'm trying to get all the images in a folder with an AJAX request (for use in an image slider). I've found this jQuery solution which works perfectly fine, except that it uses jQuery. What would a pure JS equivalent look like? (i.e. XMLHttpRequest)
Asked
Active
Viewed 4,202 times
1
-
2the server would have to return a response with the directory listing. – Daniel A. White Mar 20 '19 at 19:12
-
3Which parts exactly are you having problems with converting? Do you understand the jQuery example? – Felix Kling Mar 20 '19 at 19:13
-
@FelixKling I think I do at least. :) The line where it looks for anchors with the href attribute I find a bit weird though... The .find could be where I get stuck here. The equivalent would be a querySelectorAll pretty much right? – Rainman Mar 20 '19 at 19:22
-
@Rainman http://youmightnotneedjquery.com/ – Mar 20 '19 at 19:28
-
Great site @Amy , hanging out there right now :) – Rainman Mar 20 '19 at 19:31
2 Answers
5
Thanks to @FZs help this is what I ended up with. Thank you!
var xhr = new XMLHttpRequest();
xhr.open("GET", "/img", true);
xhr.responseType = 'document';
xhr.onload = () => {
if (xhr.status === 200) {
var elements = xhr.response.getElementsByTagName("a");
for (x of elements) {
if ( x.href.match(/\.(jpe?g|png|gif)$/) ) {
let img = document.createElement("img");
img.src = x.href;
document.body.appendChild(img);
}
};
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
}
xhr.send()

Rainman
- 159
- 2
- 7
1
You can do it without jQuery! Maybe with more code, but this should work (adapted from this post)):
var folder = "images/";
var ajax=new XMLHttpRequest()
ajax.open("GET",folder,true)
ajax.onload=function () {
var elements=(new DOMParser()).parseFromString(ajax.responseText,"text/html").getElementsByTagname("A")
for(x of elements){
if(request.status[0]==2 && x.href.match(/\.(jpe?g|png|gif)$/)) {
let img=document.createElement("IMG")
img.src=folder+x.href
document.body.appendChild(img);
}
};
}
ajax.send()
Or, you can force XMLHttpRequest
to parse document (idea from @Rainman's comment):
ajax.responseType = "document"
So the code becomes to the following:
var folder = "images/";
var ajax=new XMLHttpRequest()
ajax.open("GET",folder,true)
ajax.onload=function () {
ajax.responseType="document"
var elements=ajax.responseText.getElementsByTagname("A")
for(x of elements){
if(request.status[0]==2 && x.href.match(/\.(jpe?g|png|gif)$/)) {
let img=document.createElement("IMG")
img.src=folder+x.href
document.body.appendChild(img);
}
};
}
ajax.send()

FZs
- 16,581
- 13
- 41
- 50
-
Thank you very much for the hasty and pretty looking response :) ajax.responseXML at line 5 returns null for me though? – Rainman Mar 20 '19 at 19:44
-
1From [MDN XHR.responseXML](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Properties): *Returns a Document containing the response to the request, or null if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML or HTML.* **So, what's the content available on the requested URL?** – FZs Mar 20 '19 at 19:57
-
-
@Rainman Make a file to the folder `index.php`/`asp`/`html`/whatever you use, which contains the `` links, **OR** use a different location for the request and replace folder on the 9th line with the image folder name! Additionally, I edited my post to handle server errors – FZs Mar 20 '19 at 20:27
-
Doesn't seem to be an issue with the path or location since I get the html anchors with the correct hrefs printed out if i console log only "ajax.response". – Rainman Mar 20 '19 at 20:33
-
@Rainman OK... it's a strange issue, but you can use `.response` instead of `.responseXML`. Do you get a string or a Document object when logging `ajax.response`? – FZs Mar 20 '19 at 20:35
-
.response returns a string, so I wound have to do things differently after that – Rainman Mar 20 '19 at 20:42
-
@Rainman Is your string formatted as a valid HTML, or it only contains `` elements? – FZs Mar 20 '19 at 20:44
-
It contains an entire valid HTML document as a string that includes the anchors in the body. – Rainman Mar 20 '19 at 20:46
-
@Rainman I edited my answer again, now using [`DOMParser`](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser) to handle the situation – FZs Mar 20 '19 at 20:54
-
1I think I just solved it! By adding ajax.responseType = 'document'; before the .onload – Rainman Mar 20 '19 at 20:58
-