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