How to avoid 'Uncaught ReferenceError: ? is undefined'? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a script (config.js) at the base of every page. But, that script contains code that isn't used on every page. The console returns unused or undeclared variables as undefined.
let currentURL = document.location.href;
function redirectURL() {
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
redirectURL();
This is what's returned.
How do I fix this?

You need to check whether the redirect element exists before trying to use it.
function redirectURL() {
if (typeof redirect == 'undefined') {
return;
}
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
This will allow the function to work without error on pages that don't define redirect.

Related

So i'm currently learning javascript from codecademy.com , i have got a issue trying to solve a code about control flow [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So , i'm currently learning control flow and i have to build a program with else if conditions. The problem is that i keep getting this errors for else ifs : "Expected an identifier and saw else" and "Expected an assigment or function call and instead saw an expression". Here's the code:
var moonPhase = 'full';
if (moonPhase === 'full') {
console.log(Howwwlll!');}
else if (moonPhase === 'mostly full'); {
console.log('Arms and legs are getting hairier.');
} else if (moonPhase === 'mostly new'); {
console.log('Back on two feet');
} else {
console.log(Caution, unknown ');}
//So what did i do wrong? Thanks in advance!
Remove the ; in the if and else condition. And also console.log() have a syntax error you are missing start ' single quotes
var moonPhase = 'full';
if (moonPhase === 'full') {
console.log('Howwwlll!');
} else if (moonPhase === 'mostly full'){
console.log('Arms and legs are getting hairier.');
} else if (moonPhase === 'mostly new'){
console.log('Back on two feet');
} else {
console.log('Caution, unknown');
}

Why does JavaScript get this comparison wrong? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is happening on an angular application I'm building. If a user enters 80 into an HTML input, it always seems to get this comparison wrong.
var x = '80';
var y = 150.9800;
/* Returns incorrect answer */
if (parceFloat(x) < y) {
return true;
} else {
return false;
}
You need to use ParseFloat() not parceFloat() ...
parceFloat is not an existing function.
parceFloat() is not a function, the function is parseFloat()
A simple typo is all the error there is.

Why Javascript replace is not working in this case [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have got a string with #! and i want to replace the #! with empty .
This is my code
$(document).ready(function () {
var uri = 'ghgfhf'
if (uri.indexOf("#") > 0) {
var clean_uri = removeURLParameter(uri);
console.log(clean_uri);
}
});
function removeURLParameter(url) {
url = url.replace(/!#/,'');
return url;
}
Could you please let me know how to replace the #! with empty ('')
"!#" should be "#!"
function removeURLParameter(url) {
url = url.replace(/#!/,'');
return url;
}
See updated jsfiddle

If/Else only works for first if [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working with this, but the issue is that only the first if ever functions correctly. If I change the order, each if statement works on its own, but the logic where if the first is false, then check the second, and so on isn't working. What am I missing here?
$("#search_button").click(function(){
var table = $('#main_index').DataTable();
var search_term = $("#second_select2 option:selected").text();
var first_s = $("#first_select2 option:selected").text();
if (first_s = 'District'){
table.columns(1).search(search_term).draw();
}
else if (first_s = 'Territory'){
table.columns(2).search(search_term).draw();
}
else if (first_s = 'Region'){
table.columns(0).search(search_term).draw();
}
else {
console.log('error');
}
});
If I console.log the search_term and first_s variables, I can see them changing and correctly working. And, as I said, without the if/else statements each of these works on its own.
In all your comparisons, you need to replace = by ===
You are using the assignment operator (=) in your comparisons. you should be using the identity operator: ===.
if (first_s = 'District'){
needs to be
if (first_s === 'District'){
as do the rest of the checks.

Checking values when div is clicked [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So, I have the script below and it successfully checks the values of the inputs when I click on a div with onclick="checkInputs()" inside.
But when I click the div again, it doesn't run the code. would I have to put return false; after it?
function checkInputs() {
if (document.getElementById("emailAdressInput").Value === "ADRESSHERE" && document.getElementById("passwordInput").Value === "PassHere")
{
document.getElementById("notifier").innerHTML = "Login Successful!";
}
else
{
document.getElementById("notifier").innerHTML = "Login Unsuccessful.";
}
}
To get the value of element you should use value not Value
document.getElementById("passwordInput").value
instead of
document.getElementById("passwordInput").Value;
UPDATE
Here is your working DEMO
The strict operator === checks for type also, If you put passHere instead of PassHere it reurns false.

Categories

Resources