sup /prog/, I need to figure out how to build a text parser in javascript.
I'm searching a list of pictures with tags attached, and at this point, it's be great just to get the names of the images returned, but eventually, I'll want to generate HTML pages based on the result... I do HTML however, so, that part will be easy. I just don't do javascript.
Name:
Anonymous2007-08-12 23:36 ID:QsEL8a0S
I assume your data's already assigned to a JavaScript-variable, e.g. var images_raw =
"src1.jpg;tag1;tag2;tag3;...;tagN;
src2.jpg;tag1;tag2;tag3;...;tagN;
...
srcM.jpg;tag1;tag2;tag3;...;tagN;"
You can simply split the data with, well, "split":
images = images_raw.split("\n\n");
... and the once more for each element in "images" in a for-loop.
Name:
Anonymous2007-08-12 23:37 ID:QsEL8a0S
complete sample:
<html>
<script type="text/javascript">
// raw list image-names and tags
var images_raw =
"src1.jpg;tag1;tag2;tag3;tag4;tag5;unique1;\n\
src2.jpg;tag1;tag2;tag3;tag4;tag5;unique2;\n\
srcM.jpg;tag1;tag2;tag3;tag4;tag5;unique3;\n";
// intermediate list of each line of the images
var images_list = images_raw.split("\n");
// multi-dimensional array with the image-names and tags
images = new Array();
// let's fetch all tags in advance to make searching easier
tags = new Array();
tag_to_images = new Object();
for (var i = 0; i < images_list.length; i++) {
// creates the Array('srcI.jpg', 'tag1', 'tag2', ..., 'tagN', '')
images[i] = images_list[i].split(";");
// Add this image's index in the "images"-array to all associated tags for easy retrieval
// Ignore the first index (because it's the images name) and the last one (because it's empty)
for (var j = 1; j < images[i].length - 1; j++) {
var tag = images[i][j];
if (!tag_to_images[tag]) {
// Tag doesn't exist yet
tags.push(tag);
tag_to_images[tag] = new Array();
}
tag_to_images[tag].push(i);
}
}
// Output just for debugging, so I won't bother with correct handling of DOM
document.open();
for(tag = 0; tag < tags.length; tag++) {
tagname = tags[tag];
document.writeln('<ul>' + tagname);
for(image = 0; image < tag_to_images[tagname].length; image++) {
image_index = tag_to_images[tagname][image];
document.writeln('<li>' + images[image_index][0] + ' (' + image_index + ')</li>');
}
document.writeln('</ul>');
}
document.close();
</script>
</html>
Name:
Anonymous2007-08-12 23:37 ID:QsEL8a0S
Oh, I forgot: I SAY YOU DO IT IN JAVA CUZ C++ IS THE GREATEST BESTEST LANGUAGE EVAR
Name:
SunnyDrake2007-08-13 7:21 ID:Zuz/rkCR
well if you wanna take list from live page(like 4chan).
not error or spell checked(fix by msdn/js doc)
var img=document.getElementsByTagName("IMG");
var outbuf=new array();
for (i=0;i<img.length();i++) outbuf[]=img[i].src;
for (;i>-1;i--) document.write("<IMG SRC='"+outbuf[i]+"' /><BR>");