How can JavaScript be utilized to allow users to change the background color without affecting other users' preferences?
When allowing users to change the background color on a website, it is important to store their preferences individually to avoid affecting other users. One way to achieve this is by using JavaScript to save the user's selected color in their browser's local storage. This way, each user can have their own unique background color without impacting others. ```javascript // Get the user's selected background color from local storage const userColor = localStorage.getItem('userColor'); // Apply the user's selected background color if(userColor) { document.body.style.backgroundColor = userColor; } // Function to change the background color and save it to local storage function changeBackgroundColor(color) { document.body.style.backgroundColor = color; localStorage.setItem('userColor', color); } ```
Related Questions
- What are the differences between using mysql_real_escape_string and htmlspecialchars for data sanitization in PHP?
- What potential pitfalls should PHP beginners be aware of when working with databases and outputting data?
- Is it necessary to initialize an array in PHP before adding elements to it, and what impact does this have on error handling and notices?