I have a function like:
function testFunction( option )
{
alert(option);
}
(the actual function does more than just answer the option)
And of course, it works if you do testFunction("qwerty");, or testFunction(myvar);, where myvar is a variable.
It does not work if I do testFunction(qwerty);, where qwerty is not a variable, as expected.
I was wondering if there is a way to make the function check to see if option is a variable or string (such as "qwerty" and myvar in the examples above) and if it is continue as normal and alert the string or the variable's value.
However, if it is not a variable or string, but is an undefined variable (such as qwerty in the example above) then I would like it to alert the name of the variable (qwerty in this case).
Is this possible?
Thanks!
Some more examples:
var myvar = "1234";
testFunction("test"); //alerts "test"
testFunction(myvar); //alerts "1234"
testFunction(qwerty); //alert "qwerty"
Your problem here is that testFunction(qwerty); will not even reach the function.
Javascript cannot interpret the variable 'qwerty' as it is not defined, so it will crash right there.
Just for fun, here's a way to do what you request, by catching the error thrown when you try to interpret an undefined variable :
function testFunction( option ){
console.log(option);
}
try {
var myvar = "1234";
testFunction("test"); //alerts "test"
testFunction(myvar);
testFunction(qwerty); //alert "qwerty"
}catch(e){
if(e.message.indexOf('is not defined')!==-1){
var nd = e.message.split(' ')[0];
testFunction(nd);
}
}
JSFiddle here
Bear in mind that you should absolutely never do that, instead, try using existing variables in your programs, it works better ;)
Related
For any other types of variables, it returns "undefined". For example, even if I write something as simple as this:
function() {
var lll = 'abcd'
return lll
//return 345
//return false
}
The function will return "undefined", in both string and integer cases. But if I return just "false", it works as intended. I tried creating another "javascript code" variable in GTM, doesn't work. I have very little experience with both javascript and GTM so I have no idea what could go wrong.
Update: okay, so now I fixed the strings and integers, the problem was that it wanted a function name (and in the example I was trying to copy, there wasn't one).
Now I have another problem - the function outputs undef if I add a line:
var trackers = ga.getAll();
Any ideas?
I have defined a javascript variable but it returns undefined.
variable = "";
if(somecondition){
variable=myString.length;
}
alert(variable);
here variable returns undefined. If I keep the alert inside the if condition, I am able to get the value but its not working if I keep the alert outside the if condition.
Your myString does not have an property called length and hence you are getting undefined.
Usually, String, array has length property while objects don't have it.
Either you are invoking a wrong property or invoking it on a wrong data type or you have to define one yourself :P
Using var will do it:
var variable;
You need to declare and define your variable.
var variable = "";
if(somecondition) {
variable = myString.length;
}
alert(variable);
Are you missing the initialization of mystring?
Look at the following
var variable = '';
if (true) {
variable = 'Yes'
}
alert(variable);
This will show alert as Yes.
So in your case myString.length; is probably undefined
If variable isn't undefined in the function, it's because it is still equal to the initial value, "" (Empty String).
What I bet is happening, is myString is actually a number and not a string. Try the following and see if you are still having a problem:
variable = "";
if(somecondition){
variable=myString.toString().length;
}
alert(variable);
I have a script that checks whether the value of a selected element equals the span id. Everything works fine except for the variable assignment.
To be more precise: The function itself works (if i alert object.id it is displayed right) but the variable assignment doesn't. If I try to alert the variable, it says it's undefined. I'm sure it's some minor mistake and it would be very nice if someone could point it out:
var spanId = (function getId(object)
{
return object.id;
//alert(object.id);
})();
Setting the variable will not update that variable when you call it again and again. You need to restructure your code in order to update that variable.
var spanId;
function getId(object) {
spanId = object.id;
};
now spanId will have the current value.
I have a small question:
I have a function in javascript.
var func = function(variable)
{
result = variable;
}
If I call
func(rabbit);
I need the result to be "rabbit".
I cannot figure out how to put the variable between the two quotes.
Assuming rabbit is supposed to be a string:
func("rabbit");
If rabbit is actually a variable, then there's no way to do this because the variable (meaning the implementation's representation of a variable) isn't actually passed to the function, but rather its value is.
Actually there's an ancient way to retrieve the variable name.
var func = function(variable)
{
console.log(variable); // outputs "white"
console.log(arguments.callee.caller.toString().match(/func\((.*?)\)/)[1]); // outputs "rabbit"
}
rabbit = 'white';
func(rabbit);
See it running http://jsfiddle.net/Q55Rb/
You could do this
function whatever(input) {
return '\"' + input + '\"';
}
result should be in quotes
I could not tell from your question whether you wanted "rabbit" to be the property name, or the value. So here is both:
var func = function(variable) {
window[variable] = 'abc'; // Setting the variable as what is passed in
window.result = variable; // Normal
}
func('rabbit'); // Will set both window.rabbit to 'abc', and window.result to 'rabbit'
I wouldn't advise setting global variables, though. More info
Ok I thought that was my problem. Now I'm pretty sure that's not it but I have no clue what it is then.
I have this in my function:
defaults = {title :{'pan':-1,'left':0,'volume':0.30,'top':111}};
Where title is a variable from the function. But as a result I get title in defaults instead of the actual title stored in the variable named title. Do you understand me?
I've got an issue with my code apparently..
function match2(string,pattern){
var patternUpper = pattern.toUpperCase(); // Change pattern to uppercase
var stringUpper = string.toUpperCase(); // Change string to uppercase
for(var i=0;i<stringUpper.length-1;i++){
if(stringUpper.indexOf(patternUpper.charAt(i))<0)
return false;
}
return true;
}
Not sure why Firefox debugger is saying "pattern is undefined", seeing as it was defined in the function, right?
Any help is much appreciated.
Liam
EDIT: It is also doing this for string. Saying "string is undefined" if I comment out the second line of that snippet.
match2 is being called here:
alert(match2("thisisatest","ahtsit"));
The result works as expected, but the issue is causing errors further down in my program I think.
When "pattern" parameter is undefined, no matter where in which browser, it means that something is wrong with calling the function and you need to check the call stack, one step before inside the function and see what is going on there when you pass the parameters. (By the way, do not name the variables by using keywords or class names, I mean the variable named "string")
Hope it helps.
Cheers
It can only happen if you don't pass the second argument or the second argument is undefined