Thomas’ Developer Blog

February 5, 2008

The question mark in javascript

Filed under: javascript — Tags: , , , — sanzon @ 1:57 am

Alright this is just something I figured that might be useful to help people out in javascript a lot.

 Ever see code like….

var targ = e.target?e.target:e.srcElement;

Generally the question that comes to mind what the heck is that quesiton mark for? Belive it or not it’s a short hand If statement.

Basically in this case it’s like saying if e.target exist then set variable targ to e.target. If not, “ELSE”, make targ equal to e.srcElement. This is really useful for cross browser issues.

Sadly, you can’t really use it in the AJAX code all that easily. Due to how many conditions you have to test against. But it’s really useful for other issues.

Also to clear some things up:

a=(a==b)?c:d

You can also write it out like that to mean… if a = b then set to c if NOT set to d.

Well hopes that helps you guys some. Also once I finish up on this drag element script I’ll be sure to update you with a really neat cross browser script for creating a draggable inbound window. Of course we’ll see how long it takes with my busy schedule.

11 Comments »

  1. Thanks for the info……ive been wondering for a while what that was…….

    Comment by Michael — December 3, 2008 @ 11:29 am

  2. Thanks dude, this came up on a script and I was like…what the hell???

    Comment by Camilo — April 30, 2009 @ 7:10 am

  3. I was hoping it’d be something magical.

    Comment by murrietadesign — November 3, 2009 @ 7:46 am

  4. One of the idiotic syntax part of JavaScript – badly readable!

    Tx for explanation!

    These JS developers were true idiots!

    Comment by Frank — December 13, 2009 @ 2:06 pm

  5. thanks, man

    Comment by nagarajan — February 24, 2010 @ 12:32 pm

  6. thanks a lot

    Comment by مازن الرمال — March 7, 2010 @ 11:03 am

  7. Thanks, it was hard to figure out for me. At least it helps saving space, something really important :)

    Comment by Esteban — February 1, 2011 @ 1:44 am

  8. This syntax looks really retarded!But many thanks for clarifying this. I really sometimes wonder why there is not _one_ standard of programming syntax and who comes up with “let’s shorten all operations possible!”. Soon you will just need a hex editor to type in your codes like the good ol 8bit mos6510 days..

    Comment by uSer — May 2, 2011 @ 11:24 am

  9. Thank you for explanation….I was looking for it.

    Comment by Anonymous — December 2, 2011 @ 11:21 am

  10. thank you for the explanation. this came in one of the code I was trying to modify.

    in my case it was something like this:

    this.lastDir = function(dir) {
    if (this.options.rememberLastDir) {
    return dir ? this.cookie(this.lCookie, dir) : this.cookie(this.lCookie);
    }
    }

    So does this mean that if I pass the variable dir to the function and if the this.options.rememberLastDir is also true it will return “dir” as the value of the function as well as the cookie, otherwise it will just return the value of the cookie?

    Am I right?

    Comment by Anonymous — December 4, 2011 @ 1:40 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.