write a javscript script that creates small iframes (around 250 pixels in widht) for a…
- User
write a javscript script that creates small iframes (around 250 pixels in widht) for a list of websites
- Assistant
Sure! Here is a JavaScript script that creates small iframes for a list of websites:
// List of websites var websites = [ "https://www.google.com", "https://www.youtube.com", "https://www.facebook.com", "https://www.twitter.com", "https://www.stackoverflow.com" ]; // Container element to hold iframes var container = document.getElementById("container"); // Create an iframe for each website websites.forEach(function(website) { // Create and configure the iframe element var iframe = document.createElement("iframe"); iframe.src = website; iframe.width = "250"; iframe.height = "100%"; // Adjust the height as needed iframe.style.border = "1px solid black"; // Append the iframe to the container container.appendChild(iframe); });To use this script, you need to have an HTML page with a container element to hold the iframes. For example, you can add a
<div>element with an id "container" like this:<div id="container"></div>After that, place the JavaScript code in a
<script>tag either in the<head>section or just before the closing tag. - User
put them in a grid
- Assistant
To create a grid layout for the iframes, you can use CSS and adjust the styles of the container and iframes. Here's an example of how you can modify the previous JavaScript code to create a grid layout:
// List of websites var websites = [ "https://www.google.com", "https://www.youtube.com", "https://www.facebook.com", "https://www.twitter.com", "https://www.stackoverflow.com" ]; // Container element to hold iframes var container = document.getElementById("container"); // Set CSS grid styles for the container container.style.display = "grid"; container.style.gridTemplateColumns = "repeat(auto-fit, minmax(250px, 1fr))"; container.style.gridGap = "10px"; // Create an iframe for each website websites.forEach(function (website) { // Create and configure the iframe element var iframe = document.createElement("iframe"); iframe.src = website; iframe.width = "100%"; iframe.height = "100%"; // Adjust the height as needed iframe.style.border = "1px solid black"; // Append the iframe to the container container.appendChild(iframe); });