Hice una página de configuraciones que cuando presiono un botón cambia el fondo, a un color o a una imagen, creé el img localStorages y el color, mi problema es que cuando no son nulos al mismo tiempo, en lugar del el fondo que va por la imagen sirve para hacer el fondo con el "color", el código que estoy intentando es este:
if (localStorage.getItem("Color") === null) { //if Color is null if (localStorage.getItem("img") === null) {// And Image too document.body.style.backgroundImage = "url('css/Images/Instruments/pexels-pixabay-459797.jpg')"; // a image appear in the background } else { // else if Color is null but img isint document.body.style.backgroundImage = localStorage.getItem("img"); //load the image in the background } // THIS IS My problem: i dont know how to check if both arent null if(localStorage.getItem("Color") !== 'null' localStorage.getItem("img") !== 'null'){ //If Both arent null document.body.style.backgroundImage = localStorage.getItem("img"); // load the Image not the background } }};
puedes usar el operador lógico &&, así:
if (!localStorage.getItem('color') { if (!localStorage.getItem('img') { document.body.style.backgroundImage = 'url("your image url")'; } else { document.body.style.backgroundImage = localStorage.getItem('img'); } if (localStorage.getItem('color') && localStorage.getItem('img') { document.body.style.backgroundImage = localStorage.getItem('img'); } }