Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

Write me a piece of java script, that has a list of a n image urls, and whenever opening…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219’s answer.
  1. User

    Write me a piece of java script, that has a list of a n image urls, and whenever opening the page, it should show 10 random images of the list. The list is hard coded.

  2. Claude Assistant

    Here's a JavaScript solution that displays 10 random images from a hardcoded list:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Random Images</title>
        <style>
            .image-container {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
                gap: 10px;
                padding: 20px;
            }
            .image-container img {
                width: 100%;
                height: 200px;
                object-fit: cover;
                border-radius: 8px;
            }
        </style>
    </head>
    <body>
        <div id="imageContainer" class="image-container"></div>
    
        <script>
            // Hardcoded list of image URLs
            const imageUrls = [
                'https://picsum.photos/400/300?random=1',
                'https://picsum.photos/400/300?random=2',
                'https://picsum.photos/400/300?random=3',
                'https://picsum.photos/400/300?random=4',
                'https://picsum.photos/400/300?random=5',
                'https://picsum.photos/400/300?random=6',
                'https://picsum.photos/400/300?random=7',
                'https://picsum.photos/400/300?random=8',
                'https://picsum.photos/400/300?random=9',
                'https://picsum.photos/400/300?random=10',
                'https://picsum.photos/400/300?random=11',
                'https://picsum.photos/400/300?random=12',
                'https://picsum.photos/400/300?random=13',
                'https://picsum.photos/400/300?random=14',
                'https://picsum.photos/400/300?random=15',
                'https://picsum.photos/400/300?random=16',
                'https://picsum.photos/400/300?random=17',
                'https://picsum.photos/400/300?random=18',
                'https://picsum.photos/400/300?random=19',
                'https://picsum.photos/400/300?random=20'
            ];
    
            // Function to get random images
            function getRandomImages(array, count) {
                const shuffled = [...array].sort(() => 0.5 - Math.random());
                return shuffled.slice(0, count);
            }
    
            // Function to display images
            function displayRandomImages() {
                const container = document.getElementById('imageContainer');
                const randomImages = getRandomImages(imageUrls, 10);
                
                randomImages.forEach(url => {
                    const img = document.createElement('img');
                    img.src = url;
                    img.alt = 'Random image';
                    container.appendChild(img);
                });
            }
    
            // Display images when page loads
            window.addEventListener('DOMContentLoaded', displayRandomImages);
        </script>
    </body>
    </html>
    

    If you want just the JavaScript portion without the HTML:

    // Hardcoded list of image URLs
    const imageUrls = [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg',
        // ... add as many URLs as needed
        'image20.jpg'
    ];
    
    // Function to get random images without duplicates
    function getRandomImages(array, count) {
        const shuffled = [...array].sort(() => 0.5 - Math.random());
        return shuffled.slice(0, count);
    }
    
    // Display 10 random images on page load
    document.addEventListener('DOMContentLoaded', () => {
        const container = document.getElementById('imageContainer');
        const randomImages = getRandomImages(imageUrls, 10);
        
        randomImages.forEach(url => {
            const img = document.createElement('img');
            img.src = url;
            img.alt = 'Random image';
            container.appendChild(img);
        });
    });
    

    The code works by:

    1. Storing all image URLs in an array
    2. Creating a function that shuffles the array and returns the first 10 items
    3. When the page loads, it creates img elements for each selected URL and adds them to the container

    Replace the placeholder URLs with your actual image URLs. The images will be different each time you refresh the page.

  3. User

    The code you wrote is great, but the I thought about it, and I need it differently. Make a new code, where the urls are somehow stored in the dom, and the javascript does not contain the urls anymore.

  4. Claude Assistant

    Here's a solution where the URLs are stored in the DOM and the JavaScript reads them from there:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Random Images</title>
        <style>
            .image-container {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
                gap: 10px;
                padding: 20px;
            }
            .image-container img {
                width: 100%;
                height: 200px;
                object-fit: cover;
                border-radius: 8px;
            }
            /* Hide the data container */
            #imageData {
                display: none;
            }
        </style>
    </head>
    <body>
        <!-- Store URLs in hidden data elements -->
        <div id="imageData">
            <span data-image-url="https://picsum.photos/400/300?random=1"></span>
            <span data-image-url="https://picsum.photos/400/300?random=2"></span>
            <span data-image-url="https://picsum.photos/400/300?random=3"></span>
            <span data-image-url="https://picsum.photos/400/300?random=4"></span>
            <span data-image-url="https://picsum.photos/400/300?random=5"></span>
            <span data-image-url="https://picsum.photos/400/300?random=6"></span>
            <span data-image-url="https://picsum.photos/400/300?random=7"></span>
            <span data-image-url="https://picsum.photos/400/300?random=8"></span>
            <span data-image-url="https://picsum.photos/400/300?random=9"></span>
            <span data-image-url="https://picsum.photos/400/300?random=10"></span>
            <span data-image-url="https://picsum.photos/400/300?random=11"></span>
            <span data-image-url="https://picsum.photos/400/300?random=12"></span>
            <span data-image-url="https://picsum.photos/400/300?random=13"></span>
            <span data-image-url="https://picsum.photos/400/300?random=14"></span>
            <span data-image-url="https://picsum.photos/400/300?random=15"></span>
            <span data-image-url="https://picsum.photos/400/300?random=16"></span>
            <span data-image-url="https://picsum.photos/400/300?random=17"></span>
            <span data-image-url="https://picsum.photos/400/300?random=18"></span>
            <span data-image-url="https://picsum.photos/400/300?random=19"></span>
            <span data-image-url="https://picsum.photos/400/300?random=20"></span>
        </div>
    
        <!-- Container for displaying images -->
        <div id="imageContainer" class="image-container"></div>
    
        <script>
            // Function to get URLs from DOM
            function getImageUrlsFromDOM() {
                const elements = document.querySelectorAll('[data-image-url]');
                return Array.from(elements).map(el => el.getAttribute('data-image-url'));
            }
    
            // Function to get random items from array
            function getRandomItems(array, count) {
                const shuffled = [...array].sort(() => 0.5 - Math.random());
                return shuffled.slice(0, count);
            }
    
            // Function to display images
            function displayRandomImages() {
                const container = document.getElementById('imageContainer');
                const allUrls = getImageUrlsFromDOM();
                const randomUrls = getRandomItems(allUrls, 10);
                
                randomUrls.forEach(url => {
                    const img = document.createElement('img');
                    img.src = url;
                    img.alt = 'Random image';
                    container.appendChild(img);
                });
            }
    
            // Display images when page loads
            window.addEventListener('DOMContentLoaded', displayRandomImages);
        </script>
    </body>
    </html>
    

    Alternative approach using a <script> tag with JSON:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Random Images</title>
        <style>
            .image-container {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
                gap: 10px;
                padding: 20px;
            }
            .image-container img {
                width: 100%;
                height: 200px;
                object-fit: cover;
                border-radius: 8px;
            }
        </style>
    </head>
    <body>
        <!-- Store URLs as JSON in a script tag -->
        <script id="imageUrls" type="application/json">
        [
            "https://picsum.photos/400/300?random=1",
            "https://picsum.photos/400/300?random=2",
            "https://picsum.photos/400/300?random=3",
            "https://picsum.photos/400/300?random=4",
            "https://picsum.photos/400/300?random=5",
            "https://picsum.photos/400/300?random=6",
            "https://picsum.photos/400/300?random=7",
            "https://picsum.photos/400/300?random=8",
            "https://picsum.photos/400/300?random=9",
            "https://picsum.photos/400/300?random=10",
            "https://picsum.photos/400/300?random=11",
            "https://picsum.photos/400/300?random=12",
            "https://picsum.photos/400/300?random=13",
            "https://picsum.photos/400/300?random=14",
            "https://picsum.photos/400/300?random=15",
            "https://picsum.photos/400/300?random=16",
            "https://picsum.photos/400/300?random=17",
            "https://picsum.photos/400/300?random=18",
            "https://picsum.photos/400/300?random=19",
            "https://picsum.photos/400/300?random=20"
        ]
        </script>
    
        <!-- Container for displaying images -->
        <div id="imageContainer" class="image-container"></div>
    
        <script>
            // Function to get URLs from DOM
            function getImageUrlsFromDOM() {
                const scriptElement = document.getElementById('imageUrls');
                return JSON.parse(scriptElement.textContent);
            }
    
            // Function to get random items from array
            function getRandomItems(array, count) {
                const shuffled = [...array].sort(() => 0.5 - Math.random());
                return shuffled.slice(0, count);
            }
    
            // Function to display images
            function displayRandomImages() {
                const container = document.getElementById('imageContainer');
                const allUrls = getImageUrlsFromDOM();
                const randomUrls = getRandomItems(allUrls, 10);
                
                randomUrls.forEach(url => {
                    const img = document.createElement('img');
                    img.src = url;
                    img.alt = 'Random image';
                    container.appendChild(img);
                });
            }
    
            // Display images when page loads
            window.addEventListener('DOMContentLoaded', displayRandomImages);
        </script>
    </body>
    </html>
    

    Both approaches store the URLs in the DOM:

    • The first uses data attributes on hidden elements
    • The second stores them as JSON in a script tag with type="application/json"

    The JavaScript code no longer contains any URLs - it reads them from the DOM instead. You can easily modify the URLs in the HTML without touching the JavaScript code.

  5. User

    That's excellent! Now I want to create the dom (without the script part) in java, using JSoup. Can you write this code?

  6. Claude Assistant

    Here's Java code using JSoup to create the DOM structure:

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    
    public class ImagePageGenerator {
        
        public static void main(String[] args) {
            // Create the document
            Document doc = Jsoup.parse("<!DOCTYPE html><html></html>");
            
            // Set up the head
            Element head = doc.head();
            head.appendElement("meta").attr("charset", "UTF-8");
            head.appendElement("meta")
                .attr("name", "viewport")
                .attr("content", "width=device-width, initial-scale=1.0");
            head.appendElement("title").text("Random Images");
            
            // Add CSS
            Element style = head.appendElement("style");
            style.text(
                ".image-container {" +
                "    display: grid;" +
                "    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));" +
                "    gap: 10px;" +
                "    padding: 20px;" +
                "}" +
                ".image-container img {" +
                "    width: 100%;" +
                "    height: 200px;" +
                "    object-fit: cover;" +
                "    border-radius: 8px;" +
                "}" +
                "#imageData {" +
                "    display: none;" +
                "}"
            );
            
            // Set up the body
            Element body = doc.body();
            
            // Create the hidden data container
            Element imageData = body.appendElement("div").attr("id", "imageData");
            
            // Add image URLs
            String[] imageUrls = {
                "https://picsum.photos/400/300?random=1",
                "https://picsum.photos/400/300?random=2",
                "https://picsum.photos/400/300?random=3",
                "https://picsum.photos/400/300?random=4",
                "https://picsum.photos/400/300?random=5",
                "https://picsum.photos/400/300?random=6",
                "https://picsum.photos/400/300?random=7",
                "https://picsum.photos/400/300?random=8",
                "https://picsum.photos/400/300?random=9",
                "https://picsum.photos/400/300?random=10",
                "https://picsum.photos/400/300?random=11",
                "https://picsum.photos/400/300?random=12",
                "https://picsum.photos/400/300?random=13",
                "https://picsum.photos/400/300?random=14",
                "https://picsum.photos/400/300?random=15",
                "https://picsum.photos/400/300?random=16",
                "https://picsum.photos/400/300?random=17",
                "https://picsum.photos/400/300?random=18",
                "https://picsum.photos/400/300?random=19",
                "https://picsum.photos/400/300?random=20"
            };
            
            for (String url : imageUrls) {
                imageData.appendElement("span").attr("data-image-url", url);
            }
            
            // Create the image container
            body.appendElement("div")
                .attr("id", "imageContainer")
                .attr("class", "image-container");
            
            // Output the HTML
            System.out.println(doc.html());
        }
    }
    

    If you prefer the JSON approach instead:

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import java.util.Arrays;
    import java.util.List;
    
    public class ImagePageGeneratorJSON {
        
        public static void main(String[] args) {
            // Create the document
            Document doc = Jsoup.parse("<!DOCTYPE html><html></html>");
            
            // Set up the head
            Element head = doc.head();
            head.appendElement("meta").attr("charset", "UTF-8");
            head.appendElement("meta")
                .attr("name", "viewport")
                .attr("content", "width=device-width, initial-scale=1.0");
            head.appendElement("title").text("Random Images");
            
            // Add CSS
            Element style = head.appendElement("style");
            style.text(
                ".image-container {" +
                "    display: grid;" +
                "    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));" +
                "    gap: 10px;" +
                "    padding: 20px;" +
                "}" +
                ".image-container img {" +
                "    width: 100%;" +
                "    height: 200px;" +
                "    object-fit: cover;" +
                "    border-radius: 8px;" +
                "}"
            );
            
            // Set up the body
            Element body = doc.body();
            
            // Create image URLs list
            List<String> imageUrls = Arrays.asList(
                "https://picsum.photos/400/300?random=1",
                "https://picsum.photos/400/300?random=2",
                "https://picsum.photos/400/300?random=3",
                "https://picsum.photos/400/300?random=4",
                "https://picsum.photos/400/300?random=5",
                "https://picsum.photos/400/300?random=6",
                "https://picsum.photos/400/300?random=7",
                "https://picsum.photos/400/300?random=8",
                "https://picsum.photos/400/300?random=9",
                "https://picsum.photos/400/300?random=10",
                "https://picsum.photos/400/300?random=11",
                "https://picsum.photos/400/300?random=12",
                "https://picsum.photos/400/300?random=13",
                "https://picsum.photos/400/300?random=14",
                "https://picsum.photos/400/300?random=15",
                "https://picsum.photos/400/300?random=16",
                "https://picsum.photos/400/300?random=17",
                "https://picsum.photos/400/300?random=18",
                "https://picsum.photos/400/300?random=19",
                "https://picsum.photos/400/300?random=20"
            );
            
            // Convert to JSON
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String jsonUrls = gson.toJson(imageUrls);
            
            // Add JSON script tag
            Element scriptTag = body.appendElement("script");
            scriptTag.attr("id", "imageUrls");
            scriptTag.attr("type", "application/json");
            scriptTag.text(jsonUrls);
            
            // Create the image container
            body.appendElement("div")
                .attr("id", "imageContainer")
                .attr("class", "image-container");
            
            // Output the HTML
            System.out.println(doc.html());
        }
    }
    

    Both versions create the DOM structure without the JavaScript part. The first version uses data attributes, while the second uses a JSON script tag. You'll need to add JSoup to your project dependencies:

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.17.2</version>
    </dependency>
    

    And for the JSON version, you'll also need Gson:

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.10.1</version>
    </dependency>
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.