I use the JQuery dialog and from the PHP I can build some add-in to the button. To be able to add code from the server side I pass a method by parameter. The problem is that FireBug tell me that the method is not defined :
okHandler is the parameter of this method call to raise the dialog and it contain a simple alert message for the moment, later some Ajax calls. Any idea why it doesn't work?
It looks like okHandler is a string containing a function declaration, not an actual function? You have
okHandler = "function anonymous(){alert('This is a test');}";
instead of
okHandler = function(){alert('This is a test');};
As John Kugelman notes, okHandler appears to be a string. It would work better if it was a function... However, if a string it must be, then you'll need to pass it through eval() to actually execute it:
eval( "(" + okHandler + ")()" )
Is the okHandler() function loaded (as a valid JS object -- not a String) at the time you get that error?
I believe it's not alright to call something like "if (foo != null)" if foo hasn't already been declared as a variable somewhere. FireBug would complain: "okHandler is no defined."
Try something like this...
var myHandlers = {};
// Load okHandler as a member of myHandlers when applicable here...
$('#dialog'+idbox)...
"Oky": function() {
myHandlers.okHandler && myHandlers.okHandler();
...
}
}
Related
I am fetching a JS script from a server, and getting the following string back:
(function(e){function t(t){for(var r,a,o=t[0],c=t[1] .....
I want to save it as a function variable so I can call it (and pass an argument).
I tried wrapping the string with a return (as suggested here) and then use Function() constructor to call it in order get the original function, but it returns undefined.
How can I do this?
Did you try eval https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
eval is a function that evaluates even if it is a function in string
something like this
const yourVariable = eval("function fromServerResponse(){console.log('executed');}")
Let's say I have a simple function like this:
function alertMessage(message){
alert(message);
}
alertMessage("Hello World");
And when I type this function's name inside Chrome's console, I get something like this.
Is it possible to get the variable type of the parameter message? In that case, I believe it should be string, since that's the parameter type for the function alert according to W3schools.
Thanks in advance
Since JavaScript is dynamically typed - the type is determined during runtime - you are not able to see the type of that variable without executing the function and calling the unary typeof operator within the function body.
Try with typeof() like:
function alertMessage(message){
alert(typeof(message));
}
alertMessage("Hello World");
There is a JS function called typeof() that you can use to determine what a variable that is already initialized is
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
If you're looking for what alert() is expecting, you should look up the docs for that function
https://www.w3schools.com/jsref/met_win_alert.asp
JavaScript is dynamically typed, as Kristianmitk mentioned. However, you can use typeof to show what it was interpreted as.
function alertMessage(message) {
alert(message);
console.log(typeof message);
}
alertMessage("Hello world");
// console output:
// string
Hi for some reason I can't get this function to work.
function go(type)
{
location=document.category.example.options[document.category.example.selectedIndex].value
}
All i want to do is embed the type variable, the go function becomes undefined whenever I try to do something like
location=document.'+type+'.example.options[document.'+type+'.example.selectedIndex].value
What am I doing wrong?
instead of document.'+type+'.example.options[document.'+type+'.example.selectedIndex].value you should write:
document[type].example.options[document[type].example.selectedIndex].value
You can use document[type].example
You need to learn more about JavaScript syntax before you do stuff like that.
Here is a solution to your problem:
function go(type)
{
location=document[type].example.options[document[type].example.selectedIndex].value;
}
go('category');
It takes advantage of the fact that these two are equivalent:
a.b === a['b']
But obviously in the second part, b can be replaced dynamically since it is a string.
I am new to constructor functions, and I have the following in a .js file I include:
// should just get the number of customers out of the passed-in object
function myCustomers(customerObj) {
this.customerCount = function(customerObj) {
return customerObj.length;
};
}
On the .htm page which includes the .js file, I have the following:
var tmpObj = new myCustomers(custObj);
alert(tmpObj.customerCount);
What I expected was the alert to give me the number of customers, such as "1", etc. Instead, the alert contains the entire text of my function as though it were a string, like this:
function(customerObj) {
return customerObj.length;
}
I'm sure this is simple, but I have had a tough time googling for an answer, since my search contains very generic words like function, text/string, method, etc. FYI, this example has been pared down from a more complicated function, to hopefully make it easier to understand the question. I have tested this simpler function with the same result.
Thanks in advance!
It looks like in your alert, you are not actually calling the function, but instead passing the function itself to alert(). Try this:
alert(tmpObj.customerCount(custObj));
You might also want to change your myCustomers object to hang on to the object that is passed into the constructor, so you don't have to pass it in again when you call the function.
You should call the method like this:
var tmpObj = new myCustomers(custObj);
alert(tmpObj.customerCount(custObj));
Note the method parenthesis ;)
I'm not expert at all in Javascript and node.js
If I want to access to a method that is contained into a string, What should I do?
Is that possible?
For instance:
function bindJS(method, path){
var js = require(path+".js");
}
and the method I'd like to get is: js.what's_inside_method
Any idea how to do that?
Thanks!
Is method a property of js? Can you use js[method]()?
The only way to do this is with eval which is potentially unsafe if your data comes from a non-trusted source (unless it's hard-coded into the code, it's an untrusted source). So, lots of red flags, but this should work:
function bindJS(method, path){
var js = require(path+".js");
func = eval(method);
func();
}