How to check an undefined JavaScript value in Facebook?
In normal JavaScript, I can check either
if (i == undefined)
or
if (i === undefined)
or the "typeof".
However, within Facebook, it will have the api_key
appended as the prefix in undefined.
That is, it will become
if (a12345_i == a12345_undefined)
which is NOT undefined.
So, what can I do to find out if the variable is undefined or not?
Well, thanks for all answers, but I thi开发者_如何转开发nk I should emphasize one thing. I have no problem to detect the null value in a normal JavaScript environment. The probem I face is when it is with Facebook as an FBML application.
typeof(var) returns a string, you should be checking:
typeof(var) == 'undefined'
Try this ...
if (i == null) or if (i === null)
if (i == '') or if (i === '')
You can use x == null
, which will check for null
or undefined
. However, the check you do should work. While a12345_undefined
doesn't look like undefined
, it is. For instance, this would work just as well:
var notDefined;
if (x === notDefined) { // ...
As long as you never assign anything into the variable undefined
(which becomes a12345_undefined
) you should be fine. undefined
is just a normal variable in JavaScript, it just starts with undefined
value. You could use any variable without a value as a comparison for undefined
.
Hope that makes sense.
精彩评论