2

I need to check for an attribute inside the Jquery Mobile transition data object. The attribute can either be undefined or has a value of dialog or some other value.

Originally I only checked like this:

$(document).on( "pagebeforechange", function( e, data ) {
    if(  A && B && data.options.role != "dialog" ){
        // do something
        }
     });

However, this way I never enter the if-clause when data.options.role is undefined. I'm currently trying like this but am not really getting anywhere:

$(document).on( "pagebeforechange", function( e, data ) {
    if(  A && B && data.options.role != "undefined" && data.options.role != "dialog" ){
        // do something
        }
     });

Question
How can I make sure the value is queries and passes into the IF clause if it's either undefined or has a value, which is not dialog?

Thanks for help!

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
frequent
  • 24,965
  • 53
  • 166
  • 316
  • id `data.options.role` is `undefined`, then your first if statement should be hit. What are `A` and `B` looking for? – Rory McCrossan May 29 '12 at 10:41
  • data.options.fromHashChange == true and self.options._transDelta == 1 - both are ok and working correclty. Only after adding the third check for dialog, I'm not getting into the IF if "undefined" – frequent May 29 '12 at 10:44
  • possible duplicate of [Detecting an undefined object property in JavaScript](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript) and [How to check for undefined in javascript?](http://stackoverflow.com/questions/2985771/how-to-check-for-undefined-in-javascript). – Felix Kling May 29 '12 at 10:46
  • 1
    Are you sure `A` and `B` are true? You can see from the following fiddle that your original condition is hit when `.role` is undefined: http://jsfiddle.net/RoryMcCrossan/NSDgL/1 – Rory McCrossan May 29 '12 at 10:48
  • hm. seems to be another error... need more than 1sec. Thanks for the pointer! – frequent May 29 '12 at 10:58
  • @RoryMcCrossan - you were correct :-) My transdelta variable was ..."tainted"... – frequent May 29 '12 at 12:34

1 Answers1

1

If you meant undefined type of javascript, for that you need to use typeof like this:

if(  A && B && typeof data.options.role != "undefined" &&
      data.options.role != "dialog" )

You also need to make sure that A and B are coming truthy too.

Sarfraz
  • 355,543
  • 70
  • 511
  • 562