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!

6
  • id data.options.role is undefined, then your first if statement should be hit. What are A and B looking for? Commented May 29, 2012 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" Commented May 29, 2012 at 10:44
  • possible duplicate of Detecting an undefined object property in JavaScript and How to check for undefined in javascript?. Commented May 29, 2012 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: jsfiddle.net/RoryMcCrossan/NSDgL/1 Commented May 29, 2012 at 10:48
  • hm. seems to be another error... need more than 1sec. Thanks for the pointer! Commented May 29, 2012 at 10:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.