Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the question mark mean in this function?

Here's the documentation for this plugin (There's only two functions.) http://tkyk.github.com/jquery-history-plugin/#documentation

$(document).ready(function() {
    function load(num) {
        $('#content').load(num +".html");
    }

    $.history.init(function(url) {
        load(url == "" ? "1" : url);
    });

    $('#ajax-links a').live('click', function(e) {
        var url = $(this).attr('href');
        url = url.replace(/^.*#/, '');
        $.history.load(url);
        return false;
    });
});

Here's the html:

<body>
  <h1>jQuery History Plugin Ajax Sample</h1>
  <div id="ajax-links">
    <ul>
      <li><a href="#1">load 1.html</a></li>
      <li><a href="#2">load 2.html</a></li>
      <li><a href="#3">load 3.html</a></li>
    </ul>
    <div id="content"></div>
    <hr />
  </div>
  <p>[<a href="../">All samples</a>] [<a href="http://github.com/tkyk/jquery-history-plugin">Project home</a>]</p>
</body>
like image 823
user784637 Avatar asked Aug 11 '11 09:08

user784637


People also ask

What does question mark mean in an equation?

Answer and Explanation: As a mathematical symbol, a question mark most often means an unknown quantity.

What does a question mark mean in programming?

“Question mark” or “conditional” operator in JavaScript is a ternary operator that has three operands. The expression consists of three operands: the condition, value if true and value if false. The evaluation of the condition should result in either true/false or a boolean value.

What is '$' in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What is the use of question mark in C++?

The question mark operator, ?:, is also found in C++. Some people call it the ternary operator because it is the only operator in C++ (and Java) that takes three operands. If you are not familiar with it, it's works like an if-else, but combines operators.


3 Answers

load(url == "" ? "1" : url);

The question mark here is a a ternary if operation, Simply put, it is a short, inline if statement.

Expanded out, the statement would look something like this:

if (url == "")
    load("1");
else
    load(url);

If the statement before the question mark evaluates to true, then the left-hand side of the colon is used, otherwise (if it is false) the right-hand side is used. You can also nest this, though it isn't always a good idea (for readability).

like image 123
foxy Avatar answered Sep 23 '22 20:09

foxy


Its shorthand for:

If (url == ""){
   load("1");
}
else {
   load(url);
}

Ie. If url equals "" then return "1", otherwise, return url

In your example, if the url equals "" then, 1.html will be loaded, otherwise, url + ".html" will be loaded

like image 25
Curtis Avatar answered Sep 20 '22 20:09

Curtis


It is a ternary operation.

like image 38
Sjoerd Avatar answered Sep 24 '22 20:09

Sjoerd