Skip to content

Commit c0f35aa

Browse files
authored
Merge pull request #34 from troyee777/main
Add script to generate and apply random background color
2 parents 2452779 + f19e13a commit c0f35aa

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Adreeja Ghatak

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
})();

0 commit comments

Comments
 (0)