JS error. Variable boolean value not changed [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I need to implement jQuery code on Facebook API event. Facebook API code must be placed before html code and also not inside jQuery function and calling jQuery function would be anonymous function , so I decided to create global variable, the true value of which would call the function. The problem is that the value of variable is not changed thought alert() function works in the same place of code.
Variable is defined at the very top of code just after script opens
var not_logged_show_wall = false;
so as part of Facebook API there is else condition and alert works inside it, but value of the variable is not changed
else{
alert();
not_logged_show_wall = true;
}
I checked that it's not changed by another alert in js code after html code(while the code higher was before). Also tried the same thing with $(document).ready()
No error in console log. What is wrong?

My guess (without fully seeing the code) is that the facebook code is wrapped in an immediately invoking function so the variable is hoisted to the top of that function and not to the window so it's actually two different variables.
if you really want it to be global (which is usually not the best) you can explicitly add it to the window, ie.
window.not_logged_show_wall = false;
else{
alert();
window.not_logged_show_wall = true;
}

Related

JavaScript variable returns undefined from inside if / function statement [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 4 years ago.
I don't understand why my global var doesn't work inside of the if statement / function.
myBtn = ["btn01", "btn02", "btn03", "btn04"];
var i;
var btnId;
for (i = 0; i < myBtn.length; i++) {
if (document.getElementById(myBtn[i])) {
btnId = myBtn[i];
document.getElementById(btnId).addEventListener("click", function() {
btnValue = document.getElementById(btnId).value;
btnName = document.getElementById(btnId).name;
});
}
};
console.log(btnValue);
/* Then continue on to use the values from btnValue and btnName */
What its meant to do;
Check if Id on button exists in HTML page
If exist then get the following tags (id=, value=, name=)
Then have the 3 values usable outside of the if statement above.
The console.log(btnValue); displays undefined
I don’t really understand what you are trying to get at here. First of all, you need to declare your variable btnValue outside of the function and then change its value on click. Second of all, when you run this the console log runs first. Console.log is also a function and so it gets pushed to the top. Lastly even if the value changes there is nothing making it console.log again for you to get that value.... you need to register an action to the change. Possibly in the form of an observable. Or at least make it console.log on every click...

Jquery GET in plain javascript [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I call my_function on form submit. The function looks inside a text file for the number entered by the user in my_input. If this number does not exist, the function pops an alert and should stop form submit.
My problem: the alert is shown, but when I close it, the form gets submitted. My problem is similar with this thread: How do I return the response from an asynchronous call?
One of the solutions there is to do the GET in plain javascript. Can someone help please? This is too advanced for me.
function my_function() {
$.get("http://www.example.com/file.txt", function(contents) {
var my_number = document.getElementById("my_input").value;
var hasString = contents.includes(my_number);
});
console.log(hasString); // **ALWAYS RETURNS UNDEFINED**
if (hasString == false) {
alert('Number does not exist in text file!');
return false;
}
}
hasString is a local variable of the callback function provided to $.get as second argument. This is why it's undefined outside said function. Note just moving the variable declaration to the outer scope won't fix the problem, as the callback function will be executed after the check hasString == false anyway (it will be executed when the get request finishes).
Answering your question more directly: either perform the get request synchronously (and properly declare the variable in the scope you need to use it) or restructure your code to use the information retrieved by the get request only inside the callback. Read the post #abc123 mentions for more information.

why does this code execute my function soon as the document is loaded? [duplicate]

This question already has answers here:
In JavaScript, does it make a difference if I call a function with parentheses?
(5 answers)
Closed 7 years ago.
I'm trying to understand why this code below executes my function as soon as the document is loaded:
var list = document.getElementsByTagName('li');
function yep() {
window.alert('yep');
}
list[0].onclick = yep();
But this does not:
list[0].onclick = yep;
Why does () make a difference when executing a function in this situation?
The parenthesis () execute function immediately. On your second line you are assigning the value of list[0].onclick to the function name but not executing it.
Putting () after a reference to a function means that you want to call the function. Leaving them off means you just want to work with the reference to the function as a value in and of itself.
yep is a reference to a function.
yep() is a directive telling the Javascript engine to execute the yep function.
That's why one executes immediately and the other does not.

javascript variable in closure not showing value assigned when initialized by $(document).ready [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 7 years ago.
In my app, I have the following closure. It contains a var, which is initialized in $(document).ready:
var myClosure = (function() {
var thing;
$(document).ready(
function() {
thing = new ClassDefinedInSomeOtherFile();
}
)
return {
thing: thing
};
})();
As the page loads (I debug in chrome), a breakpoint placed in $(document).ready() is reached and I can see thing get assigned to an object of ClassDefinedInSomeOtherFile.
However, elements attempting to subsequently access myClosure.thing encounter errors stating that myClosure.thing is undefined (as do calls from the console to myClosure.thing). If thing was exposed by the return block in myClosure, why does it not reflect the new value assigned to it, when $(document).ready() ran?
Thanks!
you are using IIFE so gets executed immediately and return { thing: undefined}, after that when .ready event triggers, it runs and change thing, but that wont change the returned object, so you would get myClosure.thing is undefined
Solution:
$(document).ready(function() {
var myClosure = (function() {
var thing;
thing = new ClassDefinedInSomeOtherFile();
return {
thing: thing
};
})()
});

javascript variable value lost when outside object [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
This may sound a newbie question but I'm having really hard time with variable scopes in Javascript.
I have the following JS snippet:
<script>
window.IDFVPlugin.getIdentifier(function(result){ uuid = result; });
alert(uuid);
</script>
I want to use the variable uuid anywhere in the script outside the window object. uuid returns the correct value only when inside the object and the value is lost when outside. So the alert above will log an undefined variable error.
You use a callback function. Result should be used inside of callback body. If you try to use it immediately after main function call - it will not be yet available
window.IDFVPlugin.getIdentifier(function(result){
uuid = result;
alert(uuid);
});

Categories

Resources