File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Description: Generates a random color and applies it to the page background.
2+ // How to use: include this script in any HTML page, or paste into browser console.
3+
4+ (function () {
5+ function randomHexColor() {
6+ // generate a random integer between 0 and 0xFFFFFF, convert to hex, pad to 6 chars
7+ const hex = Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, '0');
8+ return `#${hex}`;
9+ }
10+
11+ function applyRandomBackground() {
12+ const color = randomHexColor();
13+ if (typeof document !== 'undefined' && document.body) {
14+ document.body.style.backgroundColor = color;
15+ }
16+ console.log('Applied background color:', color);
17+ return color;
18+ }
19+
20+ // Auto-run when loaded in a browser
21+ if (typeof window !== 'undefined') {
22+ window.addEventListener('load', () => {
23+ applyRandomBackground();
24+ });
25+ }
26+
27+ // Export for Node / manual calls
28+ if (typeof module !== 'undefined' && module.exports) {
29+ module.exports = { randomHexColor, applyRandomBackground };
30+ } else {
31+ // attach to window for easy manual testing in browser console
32+ window.__randomColor = { randomHexColor, applyRandomBackground };
33+ }
34+ })();
You can’t perform that action at this time.
0 commit comments