Write me a piece of java script, that has a list of a n image urls, and whenever opening…
- 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.
Assistant
Here's a JavaScript code that displays 10 random images from a hardcoded list of image URLs:
document.addEventListener('DOMContentLoaded', function() { // Hardcoded list of image URLs const imageUrls = [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg', 'https://example.com/image4.jpg', 'https://example.com/image5.jpg', 'https://example.com/image6.jpg', 'https://example.com/image7.jpg', 'https://example.com/image8.jpg', 'https://example.com/image9.jpg', 'https://example.com/image10.jpg', 'https://example.com/image11.jpg', 'https://example.com/image12.jpg', 'https://example.com/image13.jpg', 'https://example.com/image14.jpg', 'https://example.com/image15.jpg', 'https://example.com/image16.jpg', 'https://example.com/image17.jpg', 'https://example.com/image18.jpg', 'https://example.com/image19.jpg', 'https://example.com/image20.jpg' ]; // Function to get 10 random images from the list function getRandomImages(array, count) { const shuffled = [...array].sort(() => 0.5 - Math.random()); return shuffled.slice(0, count); } // Get 10 random images const randomImages = getRandomImages(imageUrls, 10); // Create a container for the images const container = document.createElement('div'); container.className = 'image-container'; document.body.appendChild(container); // Add the random images to the page randomImages.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'Random Image'; img.style.width = '200px'; img.style.margin = '10px'; container.appendChild(img); }); });Would you like me to explain or break down this code?
- 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.
Assistant
Here's a revised JavaScript code that retrieves image URLs stored in the DOM rather than hardcoding them in the JavaScript:
document.addEventListener('DOMContentLoaded', function() { // Get image URLs from a hidden element in the DOM const imageUrlsElement = document.getElementById('image-urls'); const imageUrls = Array.from(imageUrlsElement.querySelectorAll('li')).map(li => li.textContent); // Function to get 10 random images from the list function getRandomImages(array, count) { const shuffled = [...array].sort(() => 0.5 - Math.random()); return shuffled.slice(0, Math.min(count, array.length)); } // Get 10 random images const randomImages = getRandomImages(imageUrls, 10); // Create a container for the images const container = document.createElement('div'); container.className = 'image-container'; document.body.appendChild(container); // Add the random images to the page randomImages.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'Random Image'; img.style.width = '200px'; img.style.margin = '10px'; container.appendChild(img); }); });For this to work, you would need to include a hidden element in your HTML that contains the image URLs, like this:
<ul id="image-urls" style="display: none;"> <li>https://example.com/image1.jpg</li> <li>https://example.com/image2.jpg</li> <li>https://example.com/image3.jpg</li> <li>https://example.com/image4.jpg</li> <li>https://example.com/image5.jpg</li> <li>https://example.com/image6.jpg</li> <li>https://example.com/image7.jpg</li> <li>https://example.com/image8.jpg</li> <li>https://example.com/image9.jpg</li> <li>https://example.com/image10.jpg</li> <li>https://example.com/image11.jpg</li> <li>https://example.com/image12.jpg</li> <li>https://example.com/image13.jpg</li> <li>https://example.com/image14.jpg</li> <li>https://example.com/image15.jpg</li> </ul>Would you like me to explain or break down this code?
- User
That's excellent! Now I want to create the dom (without the script part) in java, using JSoup. Can you write this code?
Assistant
Here's Java code using JSoup to create the HTML DOM structure with the image URLs:
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class ImageGalleryGenerator { public static void main(String[] args) { // Create a new document Document doc = Jsoup.parse("<!DOCTYPE html><html><head><title>Random Image Gallery</title></head><body></body></html>"); // Add CSS for the gallery Element style = doc.head().appendElement("style"); style.text(".image-container { display: flex; flex-wrap: wrap; justify-content: center; }"); // Create the hidden list of image URLs Element imageUrlsList = doc.body().appendElement("ul"); imageUrlsList.attr("id", "image-urls"); imageUrlsList.attr("style", "display: none;"); // Add image URLs to the list String[] imageUrls = { "https://example.com/image1.jpg", "https://example.com/image2.jpg", "https://example.com/image3.jpg", "https://example.com/image4.jpg", "https://example.com/image5.jpg", "https://example.com/image6.jpg", "https://example.com/image7.jpg", "https://example.com/image8.jpg", "https://example.com/image9.jpg", "https://example.com/image10.jpg", "https://example.com/image11.jpg", "https://example.com/image12.jpg", "https://example.com/image13.jpg", "https://example.com/image14.jpg", "https://example.com/image15.jpg" }; for (String url : imageUrls) { imageUrlsList.appendElement("li").text(url); } // Add a container for the images doc.body().appendElement("div").attr("id", "gallery-container"); // Add the JavaScript to the page (reference only - you'll add this separately) Element scriptPlaceholder = doc.body().appendElement("script"); scriptPlaceholder.attr("src", "gallery.js"); // Print the generated HTML System.out.println(doc.outerHtml()); } }This Java code creates an HTML document with:
- A hidden unordered list containing all your image URLs
- A placeholder div where the images will be displayed
- A reference to an external JavaScript file (gallery.js) which would contain the JavaScript code from the previous example
Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.