Automatically translated.View original post

Write a website using pure JavaScript.

# javascript

# Web Site Writing Basics

Code The web site display page is written in javascript dom and decorated with css without using html.

Of course, building a website structure without using HTML directly, but using JavaScript DOM to create all the elements and decorate them with CSS can be done as follows:

Since you asked for code to create web pages without using HTML (meaning .html files with regular structure tags), I would assume that you have JavaScript and CSS files and will use JavaScript to create the entire infrastructure and link them to CSS.

Limitations: Usually, web browsers need an HTML file as a starting point to load JavaScript scripts and CSS stylesheets. However, the code below shows how JavaScript creates everything from the < body > tag down. You will need to run this script in a very basic HTML file (perhaps only the < script > tag) to make it work.

💻 JavaScript (DOM Manipulation)

Save this code as a file named script.js.

JavaScript

/ / Product information we will show

Const products = [

{id: 1, name: "classic t-shirt," price: 350, image: "tshirt.jpg"},

{id: 2, name: "Denim Jeans," price: 1200, image: "jeans .jpg"},

{id: 3, name: "sneaker," price: 890, image: "sneaker.jpg"},

{id: 4, name: "shoulder bag," price: 550, image: "bag.jpg"}

];

/ / Main function to create web pages

CreateProductPage () function {

Const body = document body;

/ / 1.Create a header (Header)

const header = document.createElements ('header');

Header.innerHTML = '< h1 > New Arrival Product < / h1 >';

Body. AppendChild (header);

/ / 2.Create the main container for the goods

const productContainer = document.createElements ('div');

productContainer.id = 'product-list';

Body .appendChild (productContainer);

/ / 3. Loop to create each Product Card

Products.forEach (product = > {

/ / Create a core element for each item.

const card = document.createElements ('div');

card .className = 'product-card';

card .setAttribute ('data-product-id', product.id);

/ / 3.1 Create a picture

const img = document.createElements ('img');

img.src = product.image; / / Suppose there is an image named after this in the same folder.

img.alt =;

Child card (img);

/ / 3.2 Create a Product Name

const name = document.createElements ('h2');

name.textContent =;

Child appendage card (name);

/ / 3.3 Create a price

const price = document.createElements ('p');

Price.className = 'price';

Price.textContent = '${product.price} baht';

Child appendage card (price);

/ / 3.4 Create an add button to the basket

const button = document.createElements ('button');

button.textContent = 'Add to the basket';

button.addEventListener ('click', () = > {

alert ('Add ${product.name} to the basket!');

});

Child card (button);

/ / Add the goods card to the main container.

ProductContainer. AppendChild (card);

});

/ / 4.Create a footer

const footer = document.createElements ('footer');

Footer.innerHTML = '< p > & copy; 2025 online store < / p >';

Body. AppendChild (footer);

}

/ / Call the function when the DOM load is complete.

document.addEventListener ('DOMContentLoaded', createProductPage);

/ / (for demonstration) Create a CSS link using DOM (usually in HTML).

const head = document.head;

const link = document.createElements ('link');

link.rel = 'stylesheet';

link.href = 'style.css'; / / Weld to CSS file

Head. AppendChild (link);

🎨 CSS (Styling)

Save this code as a file named style.css.

CSS

/ * Generally formatted * /

Body {

font-family: 'Arial', sans-serif;

Margins: 0;

Padding: 0;

Background-color:# f4f4f9;

Color:# 333;

Line-height: 1.6;

}

/ * Header * /

Header {

Background-color:# 007bff;

Color: white;

Padding: 20px 0;

Text-align: center;

box-shadow: 0 2px 4px rgba (0, 0, 0, 0.1);

}

Header h1 {

Margins: 0;

Font-size: 2.5em;

}

/ * Cargo item container * /

# product -list {

Display: flex; / * Flexible Cargo Arrangement * /

flex-wrap: wrap; / * New line up * /

Justify-content: center; / * arranged in the middle * /

padding: 20px;

Gap: 30px; / * Spacing between goods cards * /

}

/ * Each item card * /

.product-card {

Background-color: white;

border: 1x solid # ddd;

Border-radius: 8px;

box-shadow: 0 4px 8px rgba (0, 0, 0, 0.1);

padding: 15px;

width: 250px; / * Define fixed width * /

Text-align: center;

Transition: transform 0.2s;

}

.product-card: hover {

Transform: translateY (-5xpx); / * Slide up slightly when pointing * /

box-shadow: 0 6px 12px rgba (0, 0, 0, 0.15);

}

/ * Product picture * /

.product-card img {

width: 100%;

Height: auto;

Border-radius: 4px;

Margin-bottom: 10px;

}

/ * Product name * /

.product-card h2 {

Font-size: 1.4em;

Margins: 10px 0;

color: # 007bff;

}

/ * Price * /

.product-card .price {

Font-size: 1.2em;

Color: # e67e22; / * Orange for price * /

Font-weight: Bolt;

Margin-bottom: 15px;

}

/ * button * /

.product-card butt

2025/11/8 Edited to

... Read moreถ้ามีคนทักมาถามเรื่อง “งานเขียนโค้ด” หรืออยากจ้าง “บริการออกแบบโปรแกรมอุตสาหกรรม” สิ่งที่ผมมักเริ่มทำก่อนคือทำเดโมให้เห็นภาพเหมือนตัวอย่างหน้าเว็บแสดงสินค้า (สินค้ามาใหม่) ที่สร้างด้วย JavaScript DOM เพียวๆ + CSS เพราะเดโมเล็กๆ จะช่วยคุยงานให้ชัดมากว่าเราทำอะไรได้แค่ไหน และจะต่อยอดเป็นระบบจริงยังไง 1) ทำไมเดโมแบบ JavaScript DOM ถึงช่วยคุยงานบริการได้ - ลูกค้าจะเห็นโครงสร้างข้อมูลชัด: เรามี products เป็น array/object แล้วเอาไป render เป็นการ์ด (รูป/ชื่อ/ราคา/ปุ่ม) - ต่อให้เป็นโปรแกรมอุตสาหกรรม หลักการคล้ายกัน: “ข้อมูลจากเครื่อง/เซนเซอร์/ไฟล์” → “แสดงผลบนหน้าจอ” → “กดสั่งงาน/ยืนยัน” - ใช้เวลาน้อยในการพิสูจน์แนวทาง (Proof of Concept) ก่อนขึ้นระบบใหญ่ 2) Minimal HTML Shell ที่ควรเตรียม (เพื่อให้รันได้จริง) ถึงจะบอกว่าไม่ใช้ HTML โดยตรง แต่ยังต้องมีไฟล์เริ่มต้นอย่างน้อย 1 ไฟล์เพื่อโหลด script.js เช่น - index.html ที่มี <script src="script.js"></script> แนะนำให้เปิดผ่าน Live Server จะได้ไม่เจอปัญหาโหลดไฟล์/รีเฟรชไม่อัปเดต 3) เช็กลิสต์คุณภาพโค้ดเวลารับงานเขียนโค้ด (ผมใช้จริงตอนส่งงาน) - แยกข้อมูลกับการแสดงผล: products ควรอยู่ส่วนบนหรือแยกไฟล์ data.js - ตั้งชื่อ class/id ให้ตรงกับ CSS: ในตัวอย่างนี้ควรระวัง #product-list (อย่าเผลอพิมพ์มีเว้นวรรคเป็น #product -list ไม่งั้น CSS ไม่ติด) - ใส่ alt ให้รูป (มีแล้ว) และเตรียม fallback กรณีรูปหาย - จัดโครงสร้างให้ขยายง่าย: ถ้าจะเพิ่มหมวดหมู่/ค้นหา/เรียงราคา ให้มีฟังก์ชัน renderProducts(list) แล้วค่อยส่ง list ที่ถูกกรองเข้าไป 4) จากหน้าเว็บเดโม ไปสู่ “โปรแกรมอุตสาหกรรม” ต้องเพิ่มอะไรบ้าง ถ้าจะทำเป็นระบบใช้งานจริง (เช่น หน้าจอ HMI บนเว็บ, dashboard โรงงาน, ระบบสต็อก) ผมมองเป็นชิ้นๆ แบบนี้ - การเชื่อมข้อมูล: ดึงจาก API/ฐานข้อมูล (แทน array ในไฟล์) - ความปลอดภัย: แยกสิทธิ์ผู้ใช้ (Operator/Engineer/Admin) - บันทึกเหตุการณ์: log การกดปุ่ม/การเปลี่ยนค่า เพื่อ audit ย้อนหลัง - ความเสถียร: ออกแบบให้รีโหลดแล้วข้อมูลไม่หาย และจัดการ error ให้ผู้ใช้เข้าใจ 5) สเปกที่ควรถามก่อนตีราคา/รับงาน - ต้องการแค่หน้าเว็บแสดงผล หรือมีการกดสั่งงาน/ควบคุมอุปกรณ์ด้วย - จำนวนหน้าจอ/จำนวนข้อมูลที่แสดง (เช่น รายการสินค้า 100 รายการ vs 10,000 รายการ) - ต้องรองรับมือถือ/แท็บเล็ต/จอทีวีโรงงาน - ต้องการส่งมอบแบบไฟล์ static หรือมี backend + database ถ้าใครกำลังเริ่มจากเดโม JavaScript DOM แบบในโพสต์นี้ ผมแนะนำให้ลองเพิ่ม 2 ฟีเจอร์ง่ายๆ เพื่อให้ใกล้งานจริงขึ้น: (1) ปุ่มเพิ่มลงตะกร้าเปลี่ยนเป็นอัปเดตจำนวนในหน้าแทน alert (2) ทำระบบค้นหาชื่อสินค้าแบบพิมพ์แล้วกรองทันที จะช่วยให้เห็นภาพการต่อยอดเวลาไปรับงานเขียนโค้ดมากขึ้นครับ