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>
Answer and Explanation: As a mathematical symbol, a question mark most often means an unknown quantity.
“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.
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.
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.
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).
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
It is a ternary operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With