onclick function is undefined [duplicate] - javascript

This question already has an answer here:
Why does jsfiddle throw error that function is not defined? [duplicate]
(1 answer)
Closed 6 years ago.
I know this is probably something really dumb, but it really pisses me off for a good 15 minutes. What am I missing here?
<input type="button" value="Go" onclick="showAlert()">
function showAlert() {
alert('???');
}
Fiddle

Because in that fiddle you've chosen to define the function "onLoad".
That's same as defining it in window.onload=function(){...}, which makes it unaccessible outside the onload scope (i.e. you can call it only from that onload), as happens with any nested functions.
You should define that function in the <head>, choose the third option.

Related

Functions that call eachother [duplicate]

This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 2 years ago.
I am looking for someone to explain me something since I can't find a clear answer. My question is about functions that call themselves. I saw that it is possible to build a list of functions that are 'chained' together and for example the first function calls the second one then the second one calls another one. My confusion is : Lets say that you have a second function that has a variable let a = 12; if i call that function on the first function, will i have access to that variable or whatever that second function might have inside? How can i pass info to another function? can a function can be dependent on another function in order to complete a task? Thanks in advance guys.
Edit to make it more clear what I mean:
function first(){
second();
// will i have access to whatever there is inside function second since i am calling it here ? or it doesn't work that way?
}
function second(){
let a = 12;
third();
}
function third(){
fourth()....
}

Unexpected behaviour calling function from function [duplicate]

This question already has answers here:
What is the difference between a function call and function reference?
(6 answers)
Closed 2 years ago.
Recently I started learning about JS, and I got stuck in the following issue:
I am trying to call a function "draw_point" when a "mousemove" event happens.
When trying the following, the code works as expected (the function gets called):
svg.on('mousemove', draw_point(true));
But when replacing the caller function like this, it stops working and I don't get any error messages to troubelshoot.
svg.on('mousemove', function () {
draw_point;
});
Any explanation of what is going on?
Both examples are wrong, but for different reasons. In the first, you're calling the functiondraw_point immediately and passing its return value to the .on call. In the second, you're creating an anonymous function that wraps draw_point, but you're not calling draw_point. This is legal because javascript allows empty statements like myVar; or 3;.
What you want to do is pass the function draw_point, but not call it. It will be called when the handler runs:
svg.on('mousemove', draw_point);

How to unset chrome.windows.onRemoved.addListener? [duplicate]

This question already has an answer here:
`chrome.webRequest.onBeforeRequest.removeListener`? -- How to stop a chrome web listener
(1 answer)
Closed 6 years ago.
I have the following code in my Chrome extension to detect when windows are closed:
closeListener = chrome.windows.onRemoved.addListener(function(closed_window_id){
// something
}
How do I unset this such that the anonymous function does not fire? i.e. Something like:
chrome.windows.onRemoved.removeListener(closeListener)
ANSWER
Stephan/wOxxOms answer is correct. The function within the addListener cannot be anonymous and the removeListener syntax uses the function name (or a variable pointing to the function) to clear it.
Updated codepen: http://codepen.io/anon/pen/EgpNpz
After taking a look at your code, I see your problem. The function you put into the addListener is anonymous and needs to be set to a variable or become a named function.
function newListener() {
alert();
}
//This will add the listener
chrome.windows.onRemoved.addListener(newListener);
//This will remove it
chrome.windows.onRemoved.removeListener(newListener);

How to pass parameter to callback in javascript [duplicate]

This question already has answers here:
JavaScript: Passing parameters to a callback function
(16 answers)
Closed 6 years ago.
I would like to pass some parameter in call back like the following example. Is it a possible? Or I need to do it in another way?
function testCallback(callback, destination){
// how can I pass the destination to callback
callLib(callback)
}
function myCallback(destination){
//do something
}
callLib is an external library that accept. I guess the interface should be like this
function callLib(callback){
callback("something")
}
this one I think is not duplicate one. Sorry for my bad english. I have already change the question. see if you understand what the problem is
I played with callbacks all day to learn how to do this. Here is some example code: https://stackoverflow.com/questions/37554672/jqueryui-dialog-doesnt-stop-code-to-wait-for-user-input/37556573#37556573

JavaScript - Get calling object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.

Categories