TypeError

I dont know if its appropriate to post this here but I was wondering if someone could tell me why I get a console error for the following code. The error message says “Uncaught TypeError: Cannot read property ‘toLowerCase’ of null at script.js:58”. The function works on the website but it throws this error in the console log.

script.txt (1.7 KB)
index.txt (2.8 KB)

$(document).ready(() => {

$('.btn').on('click', () => {

    $('.destruct').slideUp(2000);

    $('.redpill').slideDown(8000);

});

$('.resto').on('click', () => {

    $('.destruct').fadeIn(2000);

    $('.redpill').hide();

    $('.bluepill').hide();

});

$('.redpill').hide();

$('.buton').on('click', () => {

    $('.destruct').slideUp(2000);

    $('.bluepill').slideDown(8000);

});

$('.bluepill').hide();

});

function pet () {

 let z = document.getElementById("pugpet");

 if(z.style.display === "none") {

     z.style.display = "block";

 } else {

     z.style.display = "none";

 }

};

function kill () {

   let y = document.getElementById("pill");

   if (y.style.display === "none") {

       y.style.display = "block";

   } else {

       y.style.display = "none";

   }

};

function toggle () {

let x = document.getElementById("pills");

if (x.style.display === "none") {

    x.style.display = "block";

} else {

    x.style.display = "none";

}

};

const message = document.getElementById(‘message’);

const param = new URLSearchParams(window.location.search);

const username = param.get(‘username’);

const pw = param.get(‘pw’);

if(username.toLowerCase() === ‘neo’ && pw.toLowerCase() === ‘simulation’) {

window.location.href = "page2.html";

} else if(!username || !pw) {

  window.location.href = "page.html";

} else if(username.toLowerCase() === ‘fluffy’ && pw.toLowerCase() === ‘woof’) {

window.location.href = "page3.html";

} else {

window.location.href = "page.html";

};

Essentially your issue is that at some point, you’re trying to set a null value to lower case. I’m not sure exactly where in your code it is happening (due to formatting my line 58 is different to whatever yours may be, but that’s where the error is occurring), but it’s likely to do with the if checks. You’re setting .toLowerCase() on username and pw which seem to be dynamically set by the webpage. Therefore if nothing is returned from these, it’ll be a null value returned, and as such this error will occur. You can either set a default value to set these values to in situations where a null is given, or if it’s not causing webpage issues you can likely ignore it.